Skip to content

Commit af98f93

Browse files
authored
Merge pull request #143 from rust-embedded-community/feature/result-propagation
Handling `Result`s throughout the crate
2 parents a4701c1 + beb6b9e commit af98f93

File tree

3 files changed

+106
-83
lines changed

3 files changed

+106
-83
lines changed

src/control_pipe.rs

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,19 @@ 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,
139-
Err(_) => {
138+
Err(UsbError::WouldBlock) => return Ok(None),
139+
Err(_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)
142+
usb_debug!("Failed EP0 read: {:?}", _err);
142143
self.set_error();
143-
return None;
144+
return Ok(None);
144145
}
145146
};
146147

@@ -154,7 +155,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
154155
if self.i >= self.len {
155156
usb_debug!("Request OUT complete: {:?}", req);
156157
self.state = ControlState::CompleteOut;
157-
return Some(req);
158+
return Ok(Some(req));
158159
}
159160
}
160161
// The host may terminate a DATA stage early by sending a zero-length status packet
@@ -167,7 +168,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
167168
"Control transfer completed. Current state: {:?}",
168169
self.state
169170
);
170-
let _ = self.ep_out.read(&mut []);
171+
self.ep_out.read(&mut [])?;
171172
self.state = ControlState::Idle;
172173
}
173174
_ => {
@@ -176,28 +177,23 @@ impl<B: UsbBus> ControlPipe<'_, B> {
176177
"Discarding EP0 data due to unexpected state. Current state: {:?}",
177178
self.state
178179
);
179-
let _ = self.ep_out.read(&mut []);
180+
self.ep_out.read(&mut [])?;
180181

181182
// Unexpected OUT packet
182183
self.set_error()
183184
}
184185
}
185186

186-
None
187+
Ok(None)
187188
}
188189

189-
pub fn handle_in_complete(&mut self) -> bool {
190+
pub fn handle_in_complete(&mut self) -> Result<bool> {
190191
match self.state {
191192
ControlState::DataIn => {
192-
self.write_in_chunk();
193+
self.write_in_chunk()?;
193194
}
194195
ControlState::DataInZlp => {
195-
if self.ep_in.write(&[]).is_err() {
196-
// There isn't much we can do if the write fails, except to wait for another
197-
// poll or for the host to resend the request.
198-
return false;
199-
}
200-
196+
self.ep_in.write(&[])?;
201197
usb_trace!("wrote EP0: ZLP");
202198
self.state = ControlState::DataInLast;
203199
}
@@ -207,7 +203,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
207203
}
208204
ControlState::StatusIn => {
209205
self.state = ControlState::Idle;
210-
return true;
206+
return Ok(true);
211207
}
212208
ControlState::Idle => {
213209
// If we received a message on EP0 while sending the last portion of an IN
@@ -221,23 +217,14 @@ impl<B: UsbBus> ControlPipe<'_, B> {
221217
}
222218
};
223219

224-
false
220+
Ok(false)
225221
}
226222

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

230226
let buffer = self.static_in_buf.unwrap_or(&self.buf);
231-
let count = match self.ep_in.write(&buffer[self.i..(self.i + count)]) {
232-
Ok(c) => c,
233-
// There isn't much we can do if the write fails, except to wait for another poll or for
234-
// the host to resend the request.
235-
Err(_err) => {
236-
usb_debug!("Failed to write EP0: {:?}", _err);
237-
return;
238-
}
239-
};
240-
227+
let count = self.ep_in.write(&buffer[self.i..(self.i + count)])?;
241228
usb_trace!("wrote EP0: {:?}", &buffer[self.i..(self.i + count)]);
242229

243230
self.i += count;
@@ -251,6 +238,8 @@ impl<B: UsbBus> ControlPipe<'_, B> {
251238
ControlState::DataInLast
252239
};
253240
}
241+
242+
Ok(())
254243
}
255244

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

265-
let _ = self.ep_in.write(&[]);
254+
self.ep_in.write(&[])?;
266255
self.state = ControlState::StatusIn;
267256
Ok(())
268257
}
@@ -304,7 +293,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
304293
self.len = min(data_len, req.length as usize);
305294
self.i = 0;
306295
self.state = ControlState::DataIn;
307-
self.write_in_chunk();
296+
self.write_in_chunk()?;
308297

309298
Ok(())
310299
}

0 commit comments

Comments
 (0)