diff --git a/nrf52-code/usb-app-solutions/src/bin/usb-2.rs b/nrf52-code/usb-app-solutions/src/bin/usb-2.rs index e315d9bb3..079f1fa72 100644 --- a/nrf52-code/usb-app-solutions/src/bin/usb-2.rs +++ b/nrf52-code/usb-app-solutions/src/bin/usb-2.rs @@ -85,19 +85,18 @@ fn on_event(usbd: &Usbd, event: Event) { wvalue ); - let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength) - .expect("Error parsing request"); + let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength); match request { - Request::GetDescriptor { + Ok(Request::GetDescriptor { descriptor: Descriptor::Device, length, - } => { + }) => { defmt::info!("GET_DESCRIPTOR Device [length={}]", length); defmt::println!("Goal reached; move to the next section"); dk::exit() } - Request::SetAddress { .. } => { + Ok(Request::SetAddress { .. }) => { // On macOS you'll get this request before the GET_DESCRIPTOR request so we // need to catch it here. We'll properly handle this request later // but for now it's OK to do nothing. diff --git a/nrf52-code/usb-app-solutions/src/bin/usb-3.rs b/nrf52-code/usb-app-solutions/src/bin/usb-3.rs index 1425cc3ae..ed14570e0 100644 --- a/nrf52-code/usb-app-solutions/src/bin/usb-3.rs +++ b/nrf52-code/usb-app-solutions/src/bin/usb-3.rs @@ -58,8 +58,8 @@ fn on_event(usbd: &Usbd, ep0in: &mut Ep0In, event: Event) { Event::UsbEp0DataDone => ep0in.end(usbd), Event::UsbEp0Setup => { - let bmrequesttype = usbd.bmrequesttype().read().0 as u8; - let brequest = usbd.brequest().read().brequest().to_bits(); + let bmrequesttype = usbd::bmrequesttype(usbd); + let brequest = usbd::brequest(usbd); let wlength = usbd::wlength(usbd); let windex = usbd::windex(usbd); let wvalue = usbd::wvalue(usbd); @@ -73,14 +73,12 @@ fn on_event(usbd: &Usbd, ep0in: &mut Ep0In, event: Event) { wvalue ); - let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength).expect( - "Error parsing request (goal achieved if GET_DESCRIPTOR Device was handled before)", - ); + let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength); match request { - Request::GetDescriptor { + Ok(Request::GetDescriptor { descriptor: Descriptor::Device, length, - } => { + }) => { defmt::info!("GET_DESCRIPTOR Device [length={}]", length); let desc = usb2::device::Descriptor { @@ -107,15 +105,15 @@ fn on_event(usbd: &Usbd, ep0in: &mut Ep0In, event: Event) { }; ep0in.start(subslice, usbd); } - Request::SetAddress { .. } => { + Ok(Request::SetAddress { .. }) => { // On macOS you'll get this request before the GET_DESCRIPTOR request so we // need to catch it here. We'll properly handle this request later // but for now it's OK to do nothing. } _ => { defmt::error!( - "unknown request (goal achieved if GET_DESCRIPTOR Device was handled before)" - ); + "unknown request (goal achieved if GET_DESCRIPTOR Device was handled before)" + ); dk::exit() } } diff --git a/nrf52-code/usb-app-solutions/src/bin/usb-4.rs b/nrf52-code/usb-app-solutions/src/bin/usb-4.rs index 4822cab6a..c0c632349 100644 --- a/nrf52-code/usb-app-solutions/src/bin/usb-4.rs +++ b/nrf52-code/usb-app-solutions/src/bin/usb-4.rs @@ -79,8 +79,8 @@ fn on_event(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State, event: Event) { /// Handle a SETUP request on EP0 fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> { - let bmrequesttype = usbd.bmrequesttype().read().0 as u8; - let brequest = usbd.brequest().read().brequest().to_bits(); + let bmrequesttype = usbd::bmrequesttype(usbd); + let brequest = usbd::brequest(usbd); let wlength = usbd::wlength(usbd); let windex = usbd::windex(usbd); let wvalue = usbd::wvalue(usbd); @@ -94,18 +94,17 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> wvalue ); - let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength) - .expect("Error parsing request"); + let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength); defmt::info!("EP0: {}", defmt::Debug2Format(&request)); - // ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log - // `Request` with `defmt` + // ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log + // `Request` with `defmt` match request { // section 9.4.3 // this request is valid in any state - Request::GetDescriptor { + Ok(Request::GetDescriptor { descriptor: Descriptor::Device, length, - } => { + }) => { let desc = usb2::device::Descriptor { bDeviceClass: 0, bDeviceProtocol: 0, @@ -130,10 +129,10 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> }; ep0in.start(subslice, usbd); } - Request::GetDescriptor { + Ok(Request::GetDescriptor { descriptor: Descriptor::Configuration { index }, length, - } => { + }) => { if index == 0 { let mut resp = heapless::Vec::::new(); @@ -169,7 +168,9 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> return Err(()); } } - Request::SetAddress { address } => { + Ok(Request::SetAddress { address }) => { + // On macOS you'll get this request before the GET_DESCRIPTOR request so we + // need to catch it here. match state { State::Default => { if let Some(address) = address { @@ -194,9 +195,21 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> // the response to this request is handled in hardware } - - // stall any other request - _ => return Err(()), + Ok(_) => { + // stall anything else + return Err(()); + } + Err(_) => { + defmt::error!( + "Failed to parse: bmrequesttype: 0b{=u8:08b}, brequest: {=u8}, wlength: {=u16}, windex: 0x{=u16:04x}, wvalue: 0x{=u16:04x}", + bmrequesttype, + brequest, + wlength, + windex, + wvalue + ); + return Err(()); + } } Ok(()) diff --git a/nrf52-code/usb-app-solutions/src/bin/usb-5.rs b/nrf52-code/usb-app-solutions/src/bin/usb-5.rs index c06423530..005becc19 100644 --- a/nrf52-code/usb-app-solutions/src/bin/usb-5.rs +++ b/nrf52-code/usb-app-solutions/src/bin/usb-5.rs @@ -79,8 +79,8 @@ fn on_event(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State, event: Event) { /// Handle a SETUP request on EP0 fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> { - let bmrequesttype = usbd.bmrequesttype().read().0 as u8; - let brequest = usbd.brequest().read().brequest().to_bits(); + let bmrequesttype = usbd::bmrequesttype(usbd); + let brequest = usbd::brequest(usbd); let wlength = usbd::wlength(usbd); let windex = usbd::windex(usbd); let wvalue = usbd::wvalue(usbd); @@ -94,8 +94,7 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> wvalue ); - let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength) - .expect("Error parsing request"); + let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength); defmt::info!("EP0: {}", defmt::Debug2Format(&request)); // ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log // `Request` with `defmt` @@ -103,10 +102,10 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> match request { // section 9.4.3 // this request is valid in any state - Request::GetDescriptor { + Ok(Request::GetDescriptor { descriptor: Descriptor::Device, length, - } => { + }) => { let desc = usb2::device::Descriptor { bDeviceClass: 0, bDeviceProtocol: 0, @@ -131,10 +130,10 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> }; ep0in.start(subslice, usbd); } - Request::GetDescriptor { + Ok(Request::GetDescriptor { descriptor: Descriptor::Configuration { index }, length, - } => { + }) => { if index == 0 { let mut resp = heapless::Vec::::new(); @@ -170,7 +169,7 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> return Err(()); } } - Request::SetAddress { address } => { + Ok(Request::SetAddress { address }) => { match state { State::Default => { if let Some(address) = address { @@ -195,7 +194,7 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> // the response to this request is handled in hardware } - Request::SetConfiguration { value } => { + Ok(Request::SetConfiguration { value }) => { match *state { // unspecified behavior State::Default => return Err(()), @@ -240,9 +239,21 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()> usbd.tasks_ep0status().write_value(1); } - - // stall any other request - _ => return Err(()), + Ok(_) => { + // stall anything else + return Err(()); + } + Err(_) => { + defmt::error!( + "Failed to parse: bmrequesttype: 0b{=u8:08b}, brequest: {=u8}, wlength: {=u16}, windex: 0x{=u16:04x}, wvalue: 0x{=u16:04x}", + bmrequesttype, + brequest, + wlength, + windex, + wvalue + ); + return Err(()); + } } Ok(()) diff --git a/nrf52-code/usb-app/src/bin/usb-2.rs b/nrf52-code/usb-app/src/bin/usb-2.rs index 60f1e30d9..54c6344a1 100644 --- a/nrf52-code/usb-app/src/bin/usb-2.rs +++ b/nrf52-code/usb-app/src/bin/usb-2.rs @@ -77,13 +77,12 @@ fn on_event(_usbd: &Usbd, event: Event) { wvalue ); - let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength) - .expect("Error parsing request"); + let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength); match request { - Request::GetDescriptor { + Ok(Request::GetDescriptor { descriptor: Descriptor::Device, length, - } => { + }) => { // TODO modify `Request::parse()` in `nrf52-code/usb-lib/src/lib.rs` // so that this branch is reached @@ -92,12 +91,11 @@ fn on_event(_usbd: &Usbd, event: Event) { defmt::println!("Goal reached; move to the next section"); dk::exit() } - Request::SetAddress { .. } => { + Ok(Request::SetAddress { .. }) => { // On macOS you'll get this request before the GET_DESCRIPTOR request so we // need to catch it here. We'll properly handle this request later // but for now it's OK to do nothing. } - #[allow(unreachable_patterns)] _ => unreachable!(), // we don't handle any other Requests } } diff --git a/nrf52-code/usb-app/src/bin/usb-3.rs b/nrf52-code/usb-app/src/bin/usb-3.rs index 576c47ef0..5d288001c 100644 --- a/nrf52-code/usb-app/src/bin/usb-3.rs +++ b/nrf52-code/usb-app/src/bin/usb-3.rs @@ -73,14 +73,12 @@ fn on_event(usbd: &Usbd, ep0in: &mut Ep0In, event: Event) { wvalue ); - let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength).expect( - "Error parsing request (goal achieved if GET_DESCRIPTOR Device was handled before)", - ); + let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength); match request { - Request::GetDescriptor { + Ok(Request::GetDescriptor { descriptor: Descriptor::Device, length, - } => { + }) => { defmt::info!("GET_DESCRIPTOR Device [length={}]", length); // TODO send back a valid device descriptor, truncated to `length` bytes @@ -88,16 +86,15 @@ fn on_event(usbd: &Usbd, ep0in: &mut Ep0In, event: Event) { let resp = []; ep0in.start(&resp, usbd); } - Request::SetAddress { .. } => { + Ok(Request::SetAddress { .. }) => { // On macOS you'll get this request before the GET_DESCRIPTOR request so we // need to catch it here. We'll properly handle this request later // but for now it's OK to do nothing. } - #[allow(unreachable_patterns)] _ => { defmt::error!( - "unknown request (goal achieved if GET_DESCRIPTOR Device was handled before)" - ); + "unknown request (goal achieved if GET_DESCRIPTOR Device was handled before)" + ); dk::exit() } } diff --git a/nrf52-code/usb-app/src/bin/usb-4.rs b/nrf52-code/usb-app/src/bin/usb-4.rs index ec919178f..b2835868c 100644 --- a/nrf52-code/usb-app/src/bin/usb-4.rs +++ b/nrf52-code/usb-app/src/bin/usb-4.rs @@ -80,8 +80,8 @@ fn on_event(usbd: &Usbd, ep0in: &mut Ep0In, state: &mut State, event: Event) { /// Handle a SETUP request on EP0 fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, _state: &mut State) -> Result<(), ()> { - let bmrequesttype = usbd.bmrequesttype().read().0 as u8; - let brequest = usbd.brequest().read().brequest() as u8; + let bmrequesttype = usbd::bmrequesttype(usbd); + let brequest = usbd::brequest(usbd); let wlength = usbd::wlength(usbd); let windex = usbd::windex(usbd); let wvalue = usbd::wvalue(usbd); @@ -95,17 +95,15 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, _state: &mut State) -> Result<(), () wvalue ); - let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength) - .expect("Error parsing request"); + let request = Request::parse(bmrequesttype, brequest, wvalue, windex, wlength); defmt::info!("EP0: {}", defmt::Debug2Format(&request)); - // ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log - // `StandardRequest` with `defmt` - + // ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log + // `Request` with `defmt` match request { - Request::GetDescriptor { + Ok(Request::GetDescriptor { descriptor: Descriptor::Device, length, - } => { + }) => { let desc = usb2::device::Descriptor { bDeviceClass: 0, bDeviceProtocol: 0, @@ -120,25 +118,25 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, _state: &mut State) -> Result<(), () idVendor: consts::USB_VID_DEMO, }; let length = usize::from(length); - let bytes = desc.bytes(); - let subslice = if length >= bytes.len() { + let desc_bytes = desc.bytes(); + let subslice = if length >= desc_bytes.len() { // they want all of it - &bytes[0..] + &desc_bytes[0..] } else { // they don't want all of it - &bytes[0..length] + &desc_bytes[0..length] }; ep0in.start(subslice, usbd); } // TODO implement Configuration descriptor - Request::GetDescriptor { + Ok(Request::GetDescriptor { // descriptor: Descriptor::Configuration { index }, descriptor: _, length: _, - } => { + }) => { return Err(()); } - Request::SetAddress { .. } => { + Ok(Request::SetAddress { .. }) => { // On macOS you'll get this request before the GET_DESCRIPTOR request so we // need to catch it here. @@ -146,8 +144,21 @@ fn ep0setup(usbd: &Usbd, ep0in: &mut Ep0In, _state: &mut State) -> Result<(), () todo!() } - // stall any other request - _ => return Err(()), + Ok(_) => { + // stall anything else + return Err(()); + } + Err(_) => { + defmt::error!( + "Failed to parse: bmrequesttype: 0b{=u8:08b}, brequest: {=u8}, wlength: {=u16}, windex: 0x{=u16:04x}, wvalue: 0x{=u16:04x}", + bmrequesttype, + brequest, + wlength, + windex, + wvalue + ); + return Err(()); + } } Ok(())