use zeroize::Zeroize;
use crate::{Command, Error, StatusWord};
const WRONG_LENGTH: u8 = 0x6C;
const MAX_RESENDS: usize = 2;
#[derive(Default)]
pub(crate) struct Resends {
copy: Option<Command>,
consecutive: usize,
}
impl Resends {
pub(crate) fn current<'a>(&'a self, command: &'a Command) -> &'a Command {
self.copy.as_ref().unwrap_or(command)
}
pub(crate) fn is_wrong_length(status: StatusWord) -> bool {
status.0.to_be_bytes()[0] == WRONG_LENGTH
}
pub(crate) fn resend_with_le(
&mut self,
command: &Command,
status: StatusWord,
) -> Result<(), Error> {
if self.consecutive == MAX_RESENDS {
return Err(Error::Status(status));
}
self.consecutive += 1;
let next = Command {
le: Some(short_le(status.0.to_be_bytes()[1])),
..self.current(command).clone()
};
self.replace(next);
Ok(())
}
pub(crate) fn replace_with(&mut self, next: Command) {
self.consecutive = 0;
self.replace(next);
}
fn replace(&mut self, next: Command) {
if let Some(mut old) = self.copy.replace(next) {
old.data.zeroize();
}
}
}
impl Drop for Resends {
fn drop(&mut self) {
if let Some(copy) = &mut self.copy {
copy.data.zeroize();
}
}
}
pub(crate) fn short_le(le: u8) -> u16 {
match le {
0 => 256,
le => le.into(),
}
}