remove wrong commandresult in nfc

This commit is contained in:
2026-09-26 16:13:32 +02:00
parent 4f251de3d5
commit ab7b9401a5
+6 -117
View File
@@ -23,118 +23,9 @@ where
struct PageParseError<const N: usize> { struct PageParseError<const N: usize> {
iteration: u8, iteration: u8,
bytes: [u8; N], bytes: [u8; N],
status_code: Option<CommandResult>, status_code: Option<pn532::ErrorCode>,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct CommandResult {
/// # WrErr
/// Set to logic 1, when data is written into the FIFO by the 80C51 during the AutoColl command or MFAuthent command or
/// if data is written into the FIFO by the 80C51 during the time between sending the last bit on the RF interface and
/// receiving the last bit on the RF interface.
write_error: bool,
/// # TempErr
/// Set to logic 1, if the internal temperature sensor detects overheating. In this case the antenna drivers are switched off automatically.
overheating: bool,
/// # RFErr
/// Set to logic 1, if in active communication mode the counterpart does not switch on the RF field in time as defined in NFCIP-1 standard.
/// Note: RFErr is only used in active communication mode. The bit RxFraming or the bit TxFraming has to be set to 01h to enable
/// this functionality.
rf_timeout: bool,
/// # BufferOvfl
/// Set to logic 1, if the 80C51 or if the internal state machine (e.g. receiver) tries to write data into the FIFO buffer
/// although the FIFO buffer is already full.
buffer_overflow: bool,
/// # CollErr
/// Set to logic 1, if a bit-collision is detected. It is set to logic 0 automatically at receiver start phase.
/// This flag is only valid during the bitwise anticollision at 106 kbit/s. During communication schemes at 212 and 424 kbit/s
/// this flag is always set to logic 0.
bit_collision: bool,
/// # CRCErr
/// Set to logic 1, if RxCRCEn in CIU_RxMode register is set to logic 1 and the CRC calculation fails. It is set to logic 0 automatically
/// at receiver start-up phase.
crc_error: bool,
/// # ParityErr
/// Set to logic 1, if the parity check has failed. It is set to logic 0 automatically at receiver start-up phase.
/// Only valid for ISO/IEC 14443A/MIFARE or NFCIP-1 communication at 106 kbit/s.
parity_error: bool,
/// # ProtocollErr
/// Set to logic 1, if one out of the following cases occurs:
/// - Set to logic 1 if the SOF is incorrect. It is set to logic 0 automatically at receiver start-up phase.
/// The bit is only valid for 106 kbit in Active and Passive Communication mode.
/// - If bit DetectSync in CIU_Mode register is set to logic 1 during FeliCa communication or Active Communication
/// with transfer speeds higher than 106 kbit, ProtocolErr is set to logic 1 in case of a byte length violation.
/// - During the AutoColl command, ProtocolErr is set to logic 1, if the Initiator bit in CIU_Control register is set to logic 1.
/// - During the MFAuthent Command, ProtocolErr is set to logic 1, if the number of bytes received in one data stream is incorrect.
/// - Set to logic 1, if the Miller Decoder detects 2 pauses below the minimum time according to the ISO/IEC 14443A definitions.
protocol_error: bool,
}
impl CommandResult {
pub fn is_error(&self) -> bool {
self.write_error
|| self.overheating
|| self.rf_timeout
|| self.buffer_overflow
|| self.bit_collision
|| self.crc_error
|| self.parity_error
|| self.protocol_error
}
}
impl From<u8> for CommandResult {
fn from(value: u8) -> Self {
let mask = |bit| 1u8 << bit;
let take_bit = |bit| (value & mask(bit)) != 0u8;
Self {
write_error: take_bit(7),
overheating: take_bit(6),
rf_timeout: take_bit(5),
buffer_overflow: take_bit(4),
bit_collision: take_bit(3),
crc_error: take_bit(2),
parity_error: take_bit(1),
protocol_error: take_bit(0),
}
}
}
impl core::fmt::Display for CommandResult {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if !self.is_error() {
return write!(f, "ok");
}
write!(f, "error: ")?;
let errors = [
(self.write_error, "write_error(7)"),
(self.overheating, "overheating(6)"),
(self.rf_timeout, "rf_timeout(5)"),
(self.buffer_overflow, "buffer_overflow(4)"),
(self.bit_collision, "bit_collision(3)"),
(self.crc_error, "crc_error(2)"),
(self.parity_error, "parity_error(1)"),
(self.protocol_error, "protocol_error(0)"),
]
.into_iter()
.filter_map(|(active, name)| active.then_some(name));
let mut first = true;
for error in errors {
let separator = if !first { ", " } else { "" };
write!(f, "{separator}{error}")?;
first = false;
}
Ok(())
}
}
impl Error for CommandResult {}
impl<const N: usize> Error for PageParseError<N> {} impl<const N: usize> Error for PageParseError<N> {}
impl<const N: usize> core::fmt::Display for PageParseError<N> { impl<const N: usize> core::fmt::Display for PageParseError<N> {
@@ -147,7 +38,7 @@ impl<const N: usize> core::fmt::Display for PageParseError<N> {
match status_code { match status_code {
Some(code) => write!( Some(code) => write!(
f, f,
"NFC pages parse error in iteration {iteration} with {code}: {bytes:02x?}" "NFC pages parse error in iteration {iteration} with code: {code:?}: {bytes:02x?}"
), ),
None => write!( None => write!(
f, f,
@@ -187,7 +78,7 @@ where
.await .await
{ {
Ok(uid) => { Ok(uid) => {
info!("PN532 detected NFC target UID: {uid:?}"); info!("PN532 detected NFC target UID: 0x{uid:x?}");
Ok(()) Ok(())
} }
Err(e) => { Err(e) => {
@@ -210,7 +101,7 @@ where
error => Err(PageParseError { error => Err(PageParseError {
iteration, iteration,
bytes: arr, bytes: arr,
status_code: error.copied().map(Into::into), status_code: Some(pn532::ErrorCode::try_from(error.copied().unwrap()).unwrap()),
}), }),
} }
} }
@@ -238,14 +129,12 @@ where
} }
}; };
log::info!("{bytes:x?}");
match Self::parse_nfc_page(i, bytes) { match Self::parse_nfc_page(i, bytes) {
Ok(data) => break data, Ok(data) => break data,
Err(e) => { Err(e) => {
error!("Parsing page failed: {e}"); error!("Parsing page failed: {e}");
if e.status_code.is_some_and(|e| e.protocol_error) { return Err("Failed to parse NFC page");
// After protocol error, all subsequent reads will fail, so we can stop trying to read more pages.
return Err("Failed to parse NFC page");
}
} }
} }
}; };