usb-2-solution.rs: cleanup, move addr setting into helper

This commit is contained in:
Lotte Steenbrink 2020-07-02 18:23:41 +02:00
parent e5728a440f
commit 351cce7c16
2 changed files with 32 additions and 30 deletions

View file

@ -6,6 +6,9 @@
use core::num::NonZeroU8; use core::num::NonZeroU8;
/// Device address assigned by the host; will be in the range 1..=127
pub type Address = NonZeroU8;
/// Standard USB request /// Standard USB request
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
pub enum Request { pub enum Request {
@ -22,7 +25,7 @@ pub enum Request {
// see section 9.4.6 of the USB specification // see section 9.4.6 of the USB specification
SetAddress { SetAddress {
/// New device address, in the range `1..=127` /// New device address, in the range `1..=127`
address: Option<NonZeroU8>, address: Option<Address>,
}, },
/// SET_CONFIGURATION /// SET_CONFIGURATION
@ -101,8 +104,6 @@ pub enum Descriptor {
// there are even more descriptor types but we don't need to support them // there are even more descriptor types but we don't need to support them
} }
/// Device address assigned by the host; will be in the range 1..=127
pub type Address = NonZeroU8;
/// The state of the USB device /// The state of the USB device
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
pub enum DeviceState { pub enum DeviceState {
@ -122,6 +123,7 @@ pub enum DeviceState {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[cfg(TODO)]
use core::num::NonZeroU8; use core::num::NonZeroU8;
use crate::{Descriptor, Request}; use crate::{Descriptor, Request};

View file

@ -6,7 +6,7 @@ use dk::{
usbd::{self, Event}, usbd::{self, Event},
}; };
use panic_log as _; // panic handler use panic_log as _; // panic handler
use usb::{Descriptor, Request, DeviceState}; use usb::{Address, Descriptor, DeviceState, Request};
#[rtic::app(device = dk)] #[rtic::app(device = dk)]
const APP: () = { const APP: () = {
@ -67,35 +67,35 @@ fn on_event(usbd: &USBD, state: &mut DeviceState, event: Event) {
wvalue wvalue
); );
// todo handle less indentedly? let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength)
if let Ok(request) = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength) { .expect("Error parsing request");
match request { match request {
Request::GetDescriptor { descriptor, length } Request::GetDescriptor { descriptor, length }
if descriptor == Descriptor::Device => if descriptor == Descriptor::Device =>
{ {
log::info!("GET_DESCRIPTOR Device [length={}]", length); log::info!("GET_DESCRIPTOR Device [length={}]", length);
log::info!("Goal reached; move to the next section"); log::info!("Goal reached; move to the next section");
dk::exit() dk::exit()
}
Request::SetAddress { address } => {
log::info!("SETUP: device state is {:?}", state);
// todo hide this in a helper?
// todo check for state configured -> err
if let Some(address) = address {
log::info!("SETUP: assigning address {}", address);
*state = DeviceState::Address(address);
} else {
log::info!("SETUP: address was None; assigning Default");
*state = DeviceState::Default;
}
}
_ => unreachable!(), // we don't handle any other Requests
} }
} else { Request::SetAddress { address } => set_device_address(state, address),
unreachable!() // don't care about this for now _ => unreachable!(), // we don't handle any other Requests
} }
} }
} }
} }
fn set_device_address(state: &mut DeviceState, address: Option<Address>) {
match state {
DeviceState::Default | DeviceState::Address(..) => {
if let Some(address) = address {
log::info!("SETUP: assigning device address {}", address);
*state = DeviceState::Address(address);
} else {
log::info!("SETUP: address was None; assigning Default");
*state = DeviceState::Default;
}
}
DeviceState::Configured { .. } => panic!(), // unspecified behavior
}
}