Skip to content

Commit

Permalink
fixup, if ok: wrapper to show command in hex
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkazik committed Sep 22, 2024
1 parent 79277e9 commit b2091b4
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions usbd-storage/src/subclass/scsi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::transport::Transport;
use crate::CLASS_MASS_STORAGE;
use core::fmt::{Debug, Formatter};
use num_enum::TryFromPrimitive;
use usb_device::bus::InterfaceNumber;
use usb_device::bus::UsbBus;
Expand Down Expand Up @@ -40,16 +41,33 @@ const WRITE_10: u8 = 0x2A;
/* MMC */
const READ_FORMAT_CAPACITIES: u8 = 0x23;

/// A u8, when printed shown as hex
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct U8Hex(u8);

impl Debug for U8Hex {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "0x{:02x}", self.0)
}
}

#[cfg(feature = "defmt")]
impl defmt::Format for U8Hex {
fn format(&self, fmt: defmt::Formatter) {
defmt::write!(fmt, "0x{:02x}", self.0)
}
}

/// SCSI command
///
/// Refer to specifications (SPC,SAM,SBC,MMC,etc.)
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum ScsiCommand {
/// Note: When debugging it's shown as decimal but all commands are usually documented in hex.
Unknown {
cmd: u8,
cmd: U8Hex,
},

/* SPC */
Expand Down Expand Up @@ -154,7 +172,7 @@ fn parse_cb(cb: &[u8]) -> ScsiCommand {
READ_FORMAT_CAPACITIES => ScsiCommand::ReadFormatCapacities {
alloc_len: u16::from_be_bytes([cb[7], cb[8]]),
},
cmd => ScsiCommand::Unknown { cmd },
cmd => ScsiCommand::Unknown { cmd: U8Hex(cmd) },
}
}

Expand Down

0 comments on commit b2091b4

Please sign in to comment.