From 5494c0d6c015e1ba61287ac62e46170b5af700c1 Mon Sep 17 00:00:00 2001 From: Daniel Kenyon-Jones Date: Sun, 17 Nov 2024 16:25:56 +0000 Subject: [PATCH 1/2] add CI and defmt feature --- .github/dependabot.yml | 10 ++ .github/workflows/audit.yml | 18 +++ .github/workflows/examples_rp2040_build.yml | 37 +++++ .github/workflows/lib_build.yml | 38 +++++ Cargo.toml | 5 +- examples/rp2040/Cargo.toml | 2 +- src/buffer.rs | 26 ++-- src/bytes.rs | 1 + src/ethernet.rs | 149 +++++++++++++------- 9 files changed, 216 insertions(+), 70 deletions(-) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/audit.yml create mode 100644 .github/workflows/examples_rp2040_build.yml create mode 100644 .github/workflows/lib_build.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..c51d499 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,10 @@ +version: 2 +updates: + - package-ecosystem: "cargo" + directory: "/" + schedule: + interval: "daily" + - package-ecosystem: "cargo" + directory: "/examples/rp2040/" + schedule: + interval: "daily" diff --git a/.github/workflows/audit.yml b/.github/workflows/audit.yml new file mode 100644 index 0000000..eb3256e --- /dev/null +++ b/.github/workflows/audit.yml @@ -0,0 +1,18 @@ +name: Security audit + +on: + schedule: + - cron: "0 0 * * 0" + push: + paths: + - "**/Cargo.toml" + - "**/Cargo.lock" + +jobs: + audit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: rustsec/audit-check@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/examples_rp2040_build.yml b/.github/workflows/examples_rp2040_build.yml new file mode 100644 index 0000000..e75bf9a --- /dev/null +++ b/.github/workflows/examples_rp2040_build.yml @@ -0,0 +1,37 @@ +on: [push, pull_request] + +name: Examples RP2040 build + +env: + CARGO_TERM_COLOR: always + +jobs: + check_format_build: + name: Examples - Check, Format, Build + runs-on: ubuntu-latest + steps: + #Checkout source + - name: Checkout sources + uses: actions/checkout@v4 + #toolchain and tools + - name: Install stable toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + target: thumbv6m-none-eabi + components: rustfmt, clippy + - name: Install flip-link linker + run: cargo install flip-link + #build and lint + - name: Run cargo check - examples rp2040 + working-directory: ./examples/rp2040/ + run: cargo check + - name: Run cargo fmt - examples rp2040 + working-directory: ./examples/rp2040/ + run: cargo fmt --all -- --check + - name: Run cargo clippy - examples rp2040 + working-directory: ./examples/rp2040/ + run: cargo clippy -- -D warnings + - name: Run cargo build - examples rp2040 + working-directory: ./examples/rp2040/ + run: cargo build diff --git a/.github/workflows/lib_build.yml b/.github/workflows/lib_build.yml new file mode 100644 index 0000000..dcc9d5a --- /dev/null +++ b/.github/workflows/lib_build.yml @@ -0,0 +1,38 @@ +on: [push, pull_request] + +name: Lib build + +env: + CARGO_TERM_COLOR: always + +jobs: + check_format_build: + name: Lib - Check, Format, Build + runs-on: ubuntu-latest + steps: + #Checkout source + - name: Checkout sources + uses: actions/checkout@v4 + #toolchain and tools + - name: Install stable toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + components: rustfmt, clippy + #build and lint + - name: Run cargo check + run: cargo check + - name: Run cargo fmt + run: cargo fmt --all -- --check + - name: Run cargo clippy + run: cargo clippy -- -D warnings + - name: Run cargo clippy with defmt + run: cargo clippy --features defmt -- -D warnings + - name: Run cargo clippy on tests + run: cargo clippy --tests -- -D warnings + - name: Run cargo test + run: cargo test + - name: Run cargo build + run: cargo build + - name: Run cargo build with defmt + run: cargo build --features defmt diff --git a/Cargo.toml b/Cargo.toml index b4422b5..8bf2714 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,9 @@ smoltcp = { version = "0.9", default-features = false, features = [ "medium-ethernet", "socket-udp", ] } -defmt = "0.3" +defmt = { version = "0.3", optional = true } heapless = { version = "0.7", features = ["defmt-impl"] } usb-device = { version = "0.2", features = ["defmt"] } + +[features] +defmt = ["dep:defmt", "usb-device/defmt"] diff --git a/examples/rp2040/Cargo.toml b/examples/rp2040/Cargo.toml index b2d9292..d9cbc4b 100644 --- a/examples/rp2040/Cargo.toml +++ b/examples/rp2040/Cargo.toml @@ -25,7 +25,7 @@ smoltcp = { version = "0.9.1", default-features = false, features = [ "proto-ipv4", ] } rp-pico = { version = "0.7" } -usbd-ethernet = { path = "../../" } +usbd-ethernet = { path = "../../", features = ["defmt"] } # cargo build/run [profile.dev] diff --git a/src/buffer.rs b/src/buffer.rs index 85e92aa..fdae2cb 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -1,4 +1,3 @@ -use defmt::error; // TODO remove usb stuff from this mod use usb_device::Result; use usb_device::UsbError; @@ -44,14 +43,15 @@ impl<'a, const LEN: usize> RWBuffer<'a, LEN> { len: usize, f: impl FnOnce(&mut [u8]) -> Result<(usize, R)>, ) -> Result<(usize, R)> { - let Some(buf) = self.store.get_mut(self.write_ptr .. self.write_ptr + len) - else{ - error!("buffer: tried to write more data than capacity"); + let Some(buf) = self.store.get_mut(self.write_ptr..self.write_ptr + len) else { + #[cfg(feature = "defmt")] + defmt::error!("buffer: tried to write more data than capacity"); return Err(UsbError::BufferOverflow); }; let (written, r) = f(buf)?; if written > len { - error!("buffer: claim to have written more data than allocated"); + #[cfg(feature = "defmt")] + defmt::error!("buffer: claim to have written more data than allocated"); return Err(UsbError::BufferOverflow); } self.write_ptr += written; @@ -68,18 +68,16 @@ impl<'a, const LEN: usize> RWBuffer<'a, LEN> { len: usize, f: impl FnOnce(&mut [u8]) -> Result<(usize, R)>, ) -> Result<(usize, R)> { - let Some(buf) = self - .store - .get_mut(self.read_ptr..self.read_ptr + len) - else - { - error!("buffer: tried to read more data than available"); - return Err(UsbError::InvalidState); - }; + let Some(buf) = self.store.get_mut(self.read_ptr..self.read_ptr + len) else { + #[cfg(feature = "defmt")] + defmt::error!("buffer: tried to read more data than available"); + return Err(UsbError::InvalidState); + }; let (read, r) = f(buf)?; if read > len { - error!("buffer: claim to have read more data than allocated"); + #[cfg(feature = "defmt")] + defmt::error!("buffer: claim to have read more data than allocated"); return Err(UsbError::BufferOverflow); } self.read_ptr += read; diff --git a/src/bytes.rs b/src/bytes.rs index 1f1afaf..280291d 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -33,6 +33,7 @@ pub(crate) trait Buf { fn get_slice(&mut self, size: usize) -> &[u8]; + #[allow(dead_code)] fn get_u8(&mut self) -> Option { const SIZE: usize = core::mem::size_of::(); let value = self.chunk().first().copied()?; diff --git a/src/ethernet.rs b/src/ethernet.rs index 7e752cb..d790512 100644 --- a/src/ethernet.rs +++ b/src/ethernet.rs @@ -2,7 +2,6 @@ use super::buffer::RWBuffer; use super::bytes::{Buf, BufMut}; -use defmt::{debug, error, info, warn, Format}; use heapless::String; use smoltcp::phy::{self, Device, DeviceCapabilities, Medium}; #[allow(clippy::wildcard_imports)] @@ -37,7 +36,8 @@ const SIG_NDP_WITH_FCS: &[u8; 4] = b"NCM1"; const REQ_TYPE_DEVICE_TO_HOST: u8 = 0xA1; -#[derive(Format, PartialEq, Eq, Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +#[derive(PartialEq, Eq, Clone, Copy)] pub enum DeviceState { Disabled, Disconnected, @@ -56,7 +56,8 @@ enum HostNotification { Speed(Speed), } -#[derive(Format, PartialEq, Eq, Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +#[derive(PartialEq, Eq, Clone, Copy)] pub struct Speed { pub upload_bit_rate: u32, pub download_bit_rate: u32, @@ -131,7 +132,8 @@ impl<'a, B: UsbBus> Ethernet<'a, B> { const NOTE_TYPE_NETWORK_CONNECTION: u8 = 0x00; if self.state == DeviceState::Disabled { - warn!("ethernet: device can't change connection state while disabled",); + #[cfg(feature = "defmt")] + defmt::warn!("ethernet: device can't change connection state while disabled",); return Err(UsbError::WouldBlock); } @@ -151,9 +153,11 @@ impl<'a, B: UsbBus> Ethernet<'a, B> { if result.is_ok() { if connect { - debug!("ethernet: connecting"); + #[cfg(feature = "defmt")] + defmt::debug!("ethernet: connecting"); } else { - debug!("ethernet: disconnecting"); + #[cfg(feature = "defmt")] + defmt::debug!("ethernet: disconnecting"); } self.request_state = HostNotificationState::InProgress(HostNotification::Connect); } @@ -169,7 +173,8 @@ impl<'a, B: UsbBus> Ethernet<'a, B> { const NOTE_TYPE_CONNECTION_SPEED_CHANGE: u8 = 0x2A; if self.state == DeviceState::Disabled { - warn!("ethernet: device can't set connection speed while disabled",); + #[cfg(feature = "defmt")] + defmt::warn!("ethernet: device can't set connection speed while disabled",); return Err(UsbError::WouldBlock); } @@ -196,7 +201,8 @@ impl<'a, B: UsbBus> Ethernet<'a, B> { upload_bit_rate, download_bit_rate, })); - debug!("ethernet: setting connection speed"); + #[cfg(feature = "defmt")] + defmt::debug!("ethernet: setting connection speed"); } result.map(drop) @@ -396,14 +402,15 @@ impl<'a, B: UsbBus> OutBuf<'a, B> { let mut data: &[u8] = data; let sig = data.get_slice(4); if sig != SIG_NTH { - warn!("ethernet: received bad NTH sig."); + #[cfg(feature = "defmt")] + defmt::warn!("ethernet: received bad NTH sig."); return Err(UsbError::ParseError); } data.advance(6); // wHeaderLength, wSequence, wBlockLength - let Some(ndp_idx) = data.get_u16_le().map(usize::from) - else{ - warn!("ethernet: NTH too short, unable to read ndp_idx"); + let Some(ndp_idx) = data.get_u16_le().map(usize::from) else { + #[cfg(feature = "defmt")] + defmt::warn!("ethernet: NTH too short, unable to read ndp_idx"); return Err(UsbError::ParseError); }; assert!(!data.has_remaining()); @@ -411,7 +418,8 @@ impl<'a, B: UsbBus> OutBuf<'a, B> { }) { Ok((_, ndp_offset)) => ndp_offset, Err(UsbError::InvalidState) => { - error!("ntb: read NTB header too short NTB"); + #[cfg(feature = "defmt")] + defmt::error!("ntb: read NTB header too short NTB"); self.buffer.clear(); return Err(UsbError::ParseError); } @@ -424,35 +432,37 @@ impl<'a, B: UsbBus> OutBuf<'a, B> { // Process NTB Datagram Pointer match self.buffer.read(self.buffer.unread(), |data| { - let Some(mut ntb_datagram_pointer) = data.get(ndp_offset..ndp_offset + NDP_LEN) - else { - warn!("ethernet: NTB datagram pointer out of range or truncated"); - return Err(UsbError::ParseError); + let Some(mut ntb_datagram_pointer) = data.get(ndp_offset..ndp_offset + NDP_LEN) else { + #[cfg(feature = "defmt")] + defmt::warn!("ethernet: NTB datagram pointer out of range or truncated"); + return Err(UsbError::ParseError); }; // wdSignature let sig = ntb_datagram_pointer.get_slice(4); if sig != SIG_NDP_NO_FCS && sig != SIG_NDP_WITH_FCS { - warn!("ethernet: received bad NDP sig"); + #[cfg(feature = "defmt")] + defmt::warn!("ethernet: received bad NDP sig"); return Err(UsbError::ParseError); } ntb_datagram_pointer.advance(4); // wLength, reserved - let Some(datagram_index) = ntb_datagram_pointer.get_u16_le().map(usize::from) - else{ - warn!("ethernet: NTB too short, unable to read datagram_index"); + let Some(datagram_index) = ntb_datagram_pointer.get_u16_le().map(usize::from) else { + #[cfg(feature = "defmt")] + defmt::warn!("ethernet: NTB too short, unable to read datagram_index"); return Err(UsbError::ParseError); }; - let Some(datagram_len) = ntb_datagram_pointer.get_u16_le().map(usize::from) - else{ - warn!("ethernet: NTB too short, unable to read datagram_len"); + let Some(datagram_len) = ntb_datagram_pointer.get_u16_le().map(usize::from) else { + #[cfg(feature = "defmt")] + defmt::warn!("ethernet: NTB too short, unable to read datagram_len"); return Err(UsbError::ParseError); }; if datagram_index == 0 || datagram_len == 0 { // empty, ignore. This is allowed by the spec, so don't warn. - debug!("ethernet: empty datagram"); + #[cfg(feature = "defmt")] + defmt::debug!("ethernet: empty datagram"); return Err(UsbError::WouldBlock); } @@ -462,7 +472,8 @@ impl<'a, B: UsbBus> OutBuf<'a, B> { .get(datagram_offset..datagram_offset + datagram_len) .is_none() { - warn!("ethernet: NDP datagram pointer out of range"); + #[cfg(feature = "defmt")] + defmt::warn!("ethernet: NDP datagram pointer out of range"); return Err(UsbError::ParseError); }; @@ -592,7 +603,8 @@ impl UsbClass for Ethernet<'_, B> { writer.endpoint(&self.in_buf.write_ep)?; writer.endpoint(&self.out_buf.read_ep)?; - debug!("ethernet: configuration descriptors written"); + #[cfg(feature = "defmt")] + defmt::debug!("ethernet: configuration descriptors written"); Ok(()) } @@ -609,24 +621,30 @@ impl UsbClass for Ethernet<'_, B> { } if req.index == u16::from(u8::from(self.data_if)) { - warn!( + #[cfg(feature = "defmt")] + defmt::warn!( "ethernet: unhandled DATA_INTERFACE control_in {} {}", - req.request_type, req.request + req.request_type, + req.request ); return; } if req.index != u16::from(u8::from(self.comm_if)) { - warn!( + #[cfg(feature = "defmt")] + defmt::warn!( "ethernet: control_in unexpected interface {} - {} {}", - req.index, req.request_type, req.request + req.index, + req.request_type, + req.request ); return; } match (req.request_type, req.request) { (control::RequestType::Class, REQ_GET_NTB_PARAMETERS) => { - debug!("ethernet: REQ_GET_NTB_PARAMETERS"); + #[cfg(feature = "defmt")] + defmt::debug!("ethernet: REQ_GET_NTB_PARAMETERS"); let _: Result<()> = transfer.accept(|data| { const LEN: u16 = 28; if let Some(mut data) = data.get_mut(..LEN.into()) { @@ -650,7 +668,8 @@ impl UsbClass for Ethernet<'_, B> { }); } (control::RequestType::Class, REQ_GET_NTB_INPUT_SIZE) => { - debug!("ethernet: REQ_GET_NTB_INPUT_SIZE"); + #[cfg(feature = "defmt")] + defmt::debug!("ethernet: REQ_GET_NTB_INPUT_SIZE"); let _: Result<()> = transfer.accept(|data| { const LEN: usize = 4; @@ -665,9 +684,11 @@ impl UsbClass for Ethernet<'_, B> { }); } _ => { - warn!( + #[cfg(feature = "defmt")] + defmt::warn!( "ethernet: unhandled COMMUNICATION interface control_in {} {}", - req.request_type, req.request + req.request_type, + req.request ); } } @@ -687,26 +708,31 @@ impl UsbClass for Ethernet<'_, B> { if req.index == u16::from(u8::from(self.comm_if)) { match (req.request_type, req.request) { (control::RequestType::Class, REQ_SET_NTB_INPUT_SIZE) => { - debug!("ethernet: REQ_SET_NTB_INPUT_SIZE"); + #[cfg(feature = "defmt")] + defmt::debug!("ethernet: REQ_SET_NTB_INPUT_SIZE"); // We only support the minimum NTB maximum size the value // will always be NTB_MAX_SIZE if let Some(ntb_input_size) = transfer.data().get_u32_le() { if ntb_input_size != NTB_MAX_SIZE { - warn!( + #[cfg(feature = "defmt")] + defmt::warn!( "ncp: unexpected REQ_SET_NTB_INPUT_SIZE data {}", transfer.data() ); } } else { - warn!("ncp: unexpected REQ_SET_NTB_INPUT_SIZE data too short"); + #[cfg(feature = "defmt")] + defmt::warn!("ncp: unexpected REQ_SET_NTB_INPUT_SIZE data too short"); }; let _: Result<()> = transfer.accept(); } _ => { - warn!( + #[cfg(feature = "defmt")] + defmt::warn!( "ethernet: unhandled COMMUNICATION interface control_out {} {}", - req.request_type, req.request + req.request_type, + req.request ); } } @@ -716,34 +742,43 @@ impl UsbClass for Ethernet<'_, B> { if req.index == u16::from(u8::from(self.data_if)) { match (req.request_type, req.request) { (control::RequestType::Standard, REQ_SET_INTERFACE) => { - debug!("ethernet: REQ_SET_INTERFACE"); + #[cfg(feature = "defmt")] + defmt::debug!("ethernet: REQ_SET_INTERFACE"); if req.value == 0 { transfer.accept().ok(); self.state = DeviceState::Disabled; - info!("ethernet: data interface disabled"); + #[cfg(feature = "defmt")] + defmt::info!("ethernet: data interface disabled"); self.reset(); } else if req.value == 1 { - info!("ethernet: data interface enabled"); + #[cfg(feature = "defmt")] + defmt::info!("ethernet: data interface enabled"); self.state = DeviceState::Disconnected; transfer.accept().ok(); } else { - warn!("SET_INTERFACE out of range {}", req.request); + #[cfg(feature = "defmt")] + defmt::warn!("SET_INTERFACE out of range {}", req.request); transfer.reject().ok(); } } _ => { - warn!( + #[cfg(feature = "defmt")] + defmt::warn!( "ethernet: unhandled DATA_INTERFACE control_out {} {}", - req.request_type, req.request + req.request_type, + req.request ); } } return; } - warn!( + #[cfg(feature = "defmt")] + defmt::warn!( "ethernet: control_out unexpected interface {} - {} {}", - req.index, req.request_type, req.request + req.index, + req.request_type, + req.request ); } @@ -754,18 +789,22 @@ impl UsbClass for Ethernet<'_, B> { match self.request_state { HostNotificationState::Complete => { - warn!("ethernet: endpoint in completed when no request was in progress"); + #[cfg(feature = "defmt")] + defmt::warn!("ethernet: endpoint in completed when no request was in progress"); } HostNotificationState::InProgress(HostNotification::Connect) => { - info!("ethernet: connected"); + #[cfg(feature = "defmt")] + defmt::info!("ethernet: connected"); self.state = DeviceState::Connected; } HostNotificationState::InProgress(HostNotification::Disconnect) => { - info!("ethernet: disconnected"); + #[cfg(feature = "defmt")] + defmt::info!("ethernet: disconnected"); self.state = DeviceState::Disconnected; } HostNotificationState::InProgress(HostNotification::Speed(cs)) => { - info!("ethernet: connection speed set"); + #[cfg(feature = "defmt")] + defmt::info!("ethernet: connection speed set"); self.connection_speed = Some(cs); } } @@ -776,13 +815,15 @@ impl UsbClass for Ethernet<'_, B> { if index == self.mac_address_idx { Some(&self.mac_address) } else { - warn!("ethernet: unknown string index requested {}", index); + #[cfg(feature = "defmt")] + defmt::warn!("ethernet: unknown string index requested {}", index); None } } fn reset(&mut self) { - info!("ethernet: reset"); + #[cfg(feature = "defmt")] + defmt::info!("ethernet: reset"); self.in_buf.reset(); self.out_buf.reset(); self.state = DeviceState::Disabled; From 673881cb420a1cf4ae9115e9d1acf0a75cf82c4d Mon Sep 17 00:00:00 2001 From: Daniel Kenyon-Jones Date: Sun, 17 Nov 2024 16:46:49 +0000 Subject: [PATCH 2/2] Fix clippy issues --- examples/rp2040/.cargo/config.toml | 3 +- examples/rp2040/src/main.rs | 4 +- src/ethernet.rs | 119 ++++++++++++++--------------- src/lib.rs | 1 + 4 files changed, 63 insertions(+), 64 deletions(-) diff --git a/examples/rp2040/.cargo/config.toml b/examples/rp2040/.cargo/config.toml index 2c961fa..b806586 100644 --- a/examples/rp2040/.cargo/config.toml +++ b/examples/rp2040/.cargo/config.toml @@ -19,8 +19,7 @@ rustflags = [ # trap unreachable can save a lot of space, but requires nightly compiler. # uncomment the next line if you wish to enable it # "-Z", "trap-unreachable=no", - "-C", - "inline-threshold=5", + "-Cllvm-args=--inline-threshold=5", "-C", "no-vectorize-loops", ] diff --git a/examples/rp2040/src/main.rs b/examples/rp2040/src/main.rs index 40583c5..7f1c50f 100644 --- a/examples/rp2040/src/main.rs +++ b/examples/rp2040/src/main.rs @@ -157,12 +157,12 @@ fn usb_poll(usb_dev: &mut UsbDevice, cdc_ethernet: &mut Ethernet {} + Ok(()) | Err(UsbError::WouldBlock) => {} Err(e) => error!("Failed to set connection speed: {}", e), } } else if cdc_ethernet.state() == DeviceState::Disconnected { match cdc_ethernet.connect() { - Ok(_) | Err(UsbError::WouldBlock) => {} + Ok(()) | Err(UsbError::WouldBlock) => {} Err(e) => error!("Failed to connect: {}", e), } } diff --git a/src/ethernet.rs b/src/ethernet.rs index d790512..e2fec64 100644 --- a/src/ethernet.rs +++ b/src/ethernet.rs @@ -152,11 +152,11 @@ impl<'a, B: UsbBus> Ethernet<'a, B> { let result = self.comm_ep.write(&data); if result.is_ok() { + #[cfg(feature = "defmt")] + #[allow(clippy::if_same_then_else)] if connect { - #[cfg(feature = "defmt")] defmt::debug!("ethernet: connecting"); } else { - #[cfg(feature = "defmt")] defmt::debug!("ethernet: disconnecting"); } self.request_state = HostNotificationState::InProgress(HostNotification::Connect); @@ -277,14 +277,14 @@ impl<'a, B: UsbBus> InBuf<'a, B> { .write(len.into(), |buf| Ok((len.into(), f(buf))))?; match self.write_packet() { - Err(UsbError::WouldBlock) | Ok(_) => Ok(result), + Err(UsbError::WouldBlock) | Ok(()) => Ok(result), Err(e) => Err(e), } } fn can_write(&mut self) -> bool { match self.write_packet() { - Ok(_) => self.buffer.is_empty(), + Ok(()) => self.buffer.is_empty(), Err(_) => false, } } @@ -348,7 +348,7 @@ impl<'a, B: UsbBus> InBuf<'a, B> { impl<'a, B: UsbBus> OutBuf<'a, B> { fn can_read(&mut self) -> bool { match self.read_packet() { - Ok(_) => self.datagram_len.is_some(), + Ok(()) => self.datagram_len.is_some(), Err(_) => false, } } @@ -373,7 +373,7 @@ impl<'a, B: UsbBus> OutBuf<'a, B> { self.buffer.clear(); match self.read_packet() { - Err(UsbError::WouldBlock) | Ok(_) => Ok(result), + Err(UsbError::WouldBlock) | Ok(()) => Ok(result), Err(e) => Err(e), } } @@ -387,7 +387,7 @@ impl<'a, B: UsbBus> OutBuf<'a, B> { return Ok(()); } - let (read, _) = self + let (read, ()) = self .buffer .write(self.read_ep.max_packet_size().into(), |data| { Ok((self.read_ep.read(data)?, ())) @@ -706,70 +706,69 @@ impl UsbClass for Ethernet<'_, B> { } if req.index == u16::from(u8::from(self.comm_if)) { - match (req.request_type, req.request) { - (control::RequestType::Class, REQ_SET_NTB_INPUT_SIZE) => { - #[cfg(feature = "defmt")] - defmt::debug!("ethernet: REQ_SET_NTB_INPUT_SIZE"); - // We only support the minimum NTB maximum size the value - // will always be NTB_MAX_SIZE - if let Some(ntb_input_size) = transfer.data().get_u32_le() { - if ntb_input_size != NTB_MAX_SIZE { - #[cfg(feature = "defmt")] - defmt::warn!( - "ncp: unexpected REQ_SET_NTB_INPUT_SIZE data {}", - transfer.data() - ); - } - } else { + if let (control::RequestType::Class, REQ_SET_NTB_INPUT_SIZE) = + (req.request_type, req.request) + { + #[cfg(feature = "defmt")] + defmt::debug!("ethernet: REQ_SET_NTB_INPUT_SIZE"); + // We only support the minimum NTB maximum size the value + // will always be NTB_MAX_SIZE + if let Some(ntb_input_size) = transfer.data().get_u32_le() { + if ntb_input_size != NTB_MAX_SIZE { #[cfg(feature = "defmt")] - defmt::warn!("ncp: unexpected REQ_SET_NTB_INPUT_SIZE data too short"); - }; - - let _: Result<()> = transfer.accept(); - } - _ => { + defmt::warn!( + "ncp: unexpected REQ_SET_NTB_INPUT_SIZE data {}", + transfer.data() + ); + } + } else { #[cfg(feature = "defmt")] - defmt::warn!( - "ethernet: unhandled COMMUNICATION interface control_out {} {}", - req.request_type, - req.request - ); - } + defmt::warn!("ncp: unexpected REQ_SET_NTB_INPUT_SIZE data too short"); + }; + + let _: Result<()> = transfer.accept(); + } else { + #[cfg(feature = "defmt")] + defmt::warn!( + "ethernet: unhandled COMMUNICATION interface control_out {} {}", + req.request_type, + req.request + ); } return; } if req.index == u16::from(u8::from(self.data_if)) { - match (req.request_type, req.request) { - (control::RequestType::Standard, REQ_SET_INTERFACE) => { + if let (control::RequestType::Standard, REQ_SET_INTERFACE) = + (req.request_type, req.request) + { + #[cfg(feature = "defmt")] + defmt::debug!("ethernet: REQ_SET_INTERFACE"); + if req.value == 0 { + transfer.accept().ok(); + self.state = DeviceState::Disabled; #[cfg(feature = "defmt")] - defmt::debug!("ethernet: REQ_SET_INTERFACE"); - if req.value == 0 { - transfer.accept().ok(); - self.state = DeviceState::Disabled; - #[cfg(feature = "defmt")] - defmt::info!("ethernet: data interface disabled"); - self.reset(); - } else if req.value == 1 { - #[cfg(feature = "defmt")] - defmt::info!("ethernet: data interface enabled"); - self.state = DeviceState::Disconnected; - transfer.accept().ok(); - } else { - #[cfg(feature = "defmt")] - defmt::warn!("SET_INTERFACE out of range {}", req.request); - transfer.reject().ok(); - } - } - _ => { + defmt::info!("ethernet: data interface disabled"); + self.reset(); + } else if req.value == 1 { + #[cfg(feature = "defmt")] + defmt::info!("ethernet: data interface enabled"); + self.state = DeviceState::Disconnected; + transfer.accept().ok(); + } else { #[cfg(feature = "defmt")] - defmt::warn!( - "ethernet: unhandled DATA_INTERFACE control_out {} {}", - req.request_type, - req.request - ); + defmt::warn!("SET_INTERFACE out of range {}", req.request); + transfer.reject().ok(); } + } else { + #[cfg(feature = "defmt")] + defmt::warn!( + "ethernet: unhandled DATA_INTERFACE control_out {} {}", + req.request_type, + req.request + ); } + #[allow(clippy::needless_return)] return; } diff --git a/src/lib.rs b/src/lib.rs index 30a7e24..d32f3c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ #![warn(clippy::pedantic)] #![warn(clippy::style)] #![warn(clippy::cargo)] +#![allow(clippy::multiple_crate_versions)] #![allow(clippy::module_name_repetitions)] #![allow(clippy::missing_errors_doc)] #![allow(clippy::missing_panics_doc)]