use crate::{Chip, Identity};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Incomplete;
impl core::fmt::Display for Incomplete {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("required INQUIRY or boot-banner bytes were not returned")
}
}
#[cfg(feature = "std")]
impl std::error::Error for Incomplete {}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Evidence {
identity: Identity,
}
impl Evidence {
pub fn parse(inquiry: &[u8], banner: &[u8]) -> Result<Option<Self>, Incomplete> {
if inquiry.len() < crate::INQUIRY_LEN || banner.len() != crate::layout::BANNER.len {
return Err(Incomplete);
}
if inquiry[0] & 0x1f != 5
|| &banner[..4] != b"MT19"
|| &banner[6..12] != b" Boot "
|| Chip::from_digits(banner[4], banner[5]).is_none()
{
return Ok(None);
}
let end = banner.iter().position(|b| *b == 0).unwrap_or(banner.len());
if end <= 12 || !banner[..end].iter().all(|b| (0x20..=0x7e).contains(b)) {
return Ok(None);
}
Ok(Some(Self {
identity: Identity::parse(inquiry, banner, &[]).ok_or(Incomplete)?,
}))
}
pub fn identity(&self) -> &Identity {
&self.identity
}
}