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

Handling Results throughout the crate #143

Merged
merged 7 commits into from
Feb 29, 2024
Merged
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
51 changes: 20 additions & 31 deletions src/control_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,19 @@ impl<B: UsbBus> ControlPipe<'_, B> {
None
}

pub fn handle_out(&mut self) -> Option<Request> {
pub fn handle_out(&mut self) -> Result<Option<Request>> {
match self.state {
ControlState::DataOut(req) => {
let i = self.i;
let count = match self.ep_out.read(&mut self.buf[i..]) {
Ok(count) => count,
Err(UsbError::WouldBlock) => return None,
Err(_) => {
Err(UsbError::WouldBlock) => return Ok(None),
Err(_err) => {
// Failed to read or buffer overflow (overflow is only possible if the host
// sends more data than it indicated in the SETUP request)
usb_debug!("Failed EP0 read: {:?}", _err);
self.set_error();
return None;
return Ok(None);
}
};

Expand All @@ -154,7 +155,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
if self.i >= self.len {
usb_debug!("Request OUT complete: {:?}", req);
self.state = ControlState::CompleteOut;
return Some(req);
return Ok(Some(req));
}
}
// The host may terminate a DATA stage early by sending a zero-length status packet
Expand All @@ -167,7 +168,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
"Control transfer completed. Current state: {:?}",
self.state
);
let _ = self.ep_out.read(&mut []);
self.ep_out.read(&mut [])?;
self.state = ControlState::Idle;
}
_ => {
Expand All @@ -176,28 +177,23 @@ impl<B: UsbBus> ControlPipe<'_, B> {
"Discarding EP0 data due to unexpected state. Current state: {:?}",
self.state
);
let _ = self.ep_out.read(&mut []);
self.ep_out.read(&mut [])?;

// Unexpected OUT packet
self.set_error()
}
}

None
Ok(None)
}

pub fn handle_in_complete(&mut self) -> bool {
pub fn handle_in_complete(&mut self) -> Result<bool> {
match self.state {
ControlState::DataIn => {
self.write_in_chunk();
self.write_in_chunk()?;
}
ControlState::DataInZlp => {
if self.ep_in.write(&[]).is_err() {
// There isn't much we can do if the write fails, except to wait for another
// poll or for the host to resend the request.
return false;
}

self.ep_in.write(&[])?;
usb_trace!("wrote EP0: ZLP");
self.state = ControlState::DataInLast;
}
Expand All @@ -207,7 +203,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
}
ControlState::StatusIn => {
self.state = ControlState::Idle;
return true;
return Ok(true);
}
ControlState::Idle => {
// If we received a message on EP0 while sending the last portion of an IN
Expand All @@ -221,23 +217,14 @@ impl<B: UsbBus> ControlPipe<'_, B> {
}
};

false
Ok(false)
}

fn write_in_chunk(&mut self) {
fn write_in_chunk(&mut self) -> Result<()> {
let count = min(self.len - self.i, self.ep_in.max_packet_size() as usize);

let buffer = self.static_in_buf.unwrap_or(&self.buf);
let count = match self.ep_in.write(&buffer[self.i..(self.i + count)]) {
Ok(c) => c,
// There isn't much we can do if the write fails, except to wait for another poll or for
// the host to resend the request.
Err(_err) => {
usb_debug!("Failed to write EP0: {:?}", _err);
return;
}
};

let count = self.ep_in.write(&buffer[self.i..(self.i + count)])?;
usb_trace!("wrote EP0: {:?}", &buffer[self.i..(self.i + count)]);

self.i += count;
Expand All @@ -251,6 +238,8 @@ impl<B: UsbBus> ControlPipe<'_, B> {
ControlState::DataInLast
};
}

Ok(())
}

pub fn accept_out(&mut self) -> Result<()> {
Expand All @@ -262,7 +251,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
}
};

let _ = self.ep_in.write(&[]);
self.ep_in.write(&[])?;
self.state = ControlState::StatusIn;
Ok(())
}
Expand Down Expand Up @@ -304,7 +293,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
self.len = min(data_len, req.length as usize);
self.i = 0;
self.state = ControlState::DataIn;
self.write_in_chunk();
self.write_in_chunk()?;

Ok(())
}
Expand Down
Loading
Loading