refactor nfc reader code a bit
This commit was merged in pull request #37.
This commit is contained in:
+61
-18
@@ -1,4 +1,5 @@
|
|||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
|
use core::error::Error;
|
||||||
use embedded_hal::i2c::I2c;
|
use embedded_hal::i2c::I2c;
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use pn532::i2c::I2CInterface;
|
use pn532::i2c::I2CInterface;
|
||||||
@@ -8,6 +9,7 @@ use pn532::{Pn532, Request};
|
|||||||
pub type Pn532Device<I2C> = Pn532<I2CInterface<I2C>, (), 34>;
|
pub type Pn532Device<I2C> = Pn532<I2CInterface<I2C>, (), 34>;
|
||||||
|
|
||||||
pub const PAGES_PER_READ: usize = 4;
|
pub const PAGES_PER_READ: usize = 4;
|
||||||
|
pub const BYTES_PER_READ: usize = PAGES_PER_READ * 4;
|
||||||
|
|
||||||
/// PN532 NFC reader driver.
|
/// PN532 NFC reader driver.
|
||||||
pub struct NfcPn532Driver<I2C>
|
pub struct NfcPn532Driver<I2C>
|
||||||
@@ -17,6 +19,35 @@ where
|
|||||||
pn532: Pn532Device<I2C>,
|
pn532: Pn532Device<I2C>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct PageParseError<const N: usize> {
|
||||||
|
iteration: u8,
|
||||||
|
bytes: [u8; N],
|
||||||
|
status_code: Option<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> Error for PageParseError<N> {}
|
||||||
|
|
||||||
|
impl<const N: usize> core::fmt::Display for PageParseError<N> {
|
||||||
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||||
|
let PageParseError {
|
||||||
|
iteration,
|
||||||
|
bytes,
|
||||||
|
status_code,
|
||||||
|
} = self;
|
||||||
|
match status_code {
|
||||||
|
Some(code) => write!(
|
||||||
|
f,
|
||||||
|
"NFC pages parse error in iteration {iteration} with error code 0x{code:02x}: {bytes:02x?}"
|
||||||
|
),
|
||||||
|
None => write!(
|
||||||
|
f,
|
||||||
|
"No data available for NFC page in iteration {iteration}: {bytes:02x?}"
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<I2C> NfcPn532Driver<I2C>
|
impl<I2C> NfcPn532Driver<I2C>
|
||||||
where
|
where
|
||||||
I2C: I2c,
|
I2C: I2c,
|
||||||
@@ -57,16 +88,21 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_nfc_page(iteration: u8, bytes: &[u8]) -> Result<[u8; PAGES_PER_READ * 4], ()> {
|
fn parse_nfc_page(
|
||||||
let success = bytes.first().is_some_and(|x| *x == 0);
|
iteration: u8,
|
||||||
if success {
|
bytes: &[u8],
|
||||||
let slice_len = bytes.len().min(PAGES_PER_READ * 4 + 1);
|
) -> Result<[u8; BYTES_PER_READ], PageParseError<BYTES_PER_READ>> {
|
||||||
let mut arr = [0; PAGES_PER_READ * 4];
|
let slice_len = bytes.len().min(BYTES_PER_READ + 1);
|
||||||
|
let mut arr = [0; BYTES_PER_READ];
|
||||||
arr[..slice_len - 1].copy_from_slice(&bytes[1..slice_len]);
|
arr[..slice_len - 1].copy_from_slice(&bytes[1..slice_len]);
|
||||||
Ok(arr)
|
|
||||||
} else {
|
match bytes.first() {
|
||||||
info!("err {iteration}: {:02x?}", bytes);
|
Some(0x00) => Ok(arr),
|
||||||
Err(())
|
error => Err(PageParseError {
|
||||||
|
iteration,
|
||||||
|
bytes: arr,
|
||||||
|
status_code: error.copied(),
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,18 +115,25 @@ where
|
|||||||
let mut data: Vec<u8> = Vec::with_capacity(858);
|
let mut data: Vec<u8> = Vec::with_capacity(858);
|
||||||
|
|
||||||
for i in (0x0B..=230).step_by(PAGES_PER_READ) {
|
for i in (0x0B..=230).step_by(PAGES_PER_READ) {
|
||||||
let mut parse_result = Err(());
|
let bytes = loop {
|
||||||
while parse_result.is_err() {
|
let read = self
|
||||||
let Ok(result) = self
|
|
||||||
.pn532
|
.pn532
|
||||||
.process_async(&Request::ntag_read(i), PAGES_PER_READ * 4 + 1)
|
.process_async(&Request::ntag_read(i), BYTES_PER_READ + 1)
|
||||||
.await
|
.await;
|
||||||
else {
|
|
||||||
|
let bytes = match read {
|
||||||
|
Ok(bytes) => bytes,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Reading page {i} failed: {e:?}");
|
||||||
continue;
|
continue;
|
||||||
};
|
|
||||||
parse_result = Self::parse_nfc_page(i, result);
|
|
||||||
}
|
}
|
||||||
let bytes = parse_result.unwrap();
|
};
|
||||||
|
|
||||||
|
match Self::parse_nfc_page(i, bytes) {
|
||||||
|
Ok(data) => break data,
|
||||||
|
Err(e) => error!("Parsing page failed: {e}"),
|
||||||
|
}
|
||||||
|
};
|
||||||
let bytes = if i == 0x0B { &bytes[1..] } else { &bytes };
|
let bytes = if i == 0x0B { &bytes[1..] } else { &bytes };
|
||||||
data.extend(bytes);
|
data.extend(bytes);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user