use core::fmt;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
UnexpectedEnd,
InvalidStart(u8),
InvalidHeader,
MissingStop,
Checksum {
computed: u8,
found: u8,
},
Crc {
block: usize,
computed: u16,
found: u16,
},
InvalidLength,
TooManyExtensions,
ReservedDif(u8),
UnsupportedLvar(u8),
UnsupportedCi(u8),
OutputTooSmall,
UnexpectedLayer(u8),
UnsupportedSecurity(u8),
NoKey,
UnknownFormat(u16),
InvalidFormat,
UnexpectedFragment {
expected: u8,
found: u8,
},
FragmentFromOtherMeter,
Verification(Check),
InvalidAddress(u8),
InvalidBaud(u32),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Check {
Marker,
Mac,
PayloadCrc,
Length,
MissingCounter,
FullFrameCrc,
Tag,
}
impl fmt::Display for Check {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Marker => "decrypted payload does not start with 2F 2F",
Self::Mac => "message authentication code mismatch",
Self::PayloadCrc => "ELL payload CRC mismatch",
Self::Length => "encrypted length is not a whole number of blocks, or longer than the data",
Self::MissingCounter => "mode 7 without AFL message counter",
Self::FullFrameCrc => "compact frame does not match its full-frame CRC",
Self::Tag => "AES-CCM tag mismatch",
})
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnexpectedEnd => f.write_str("input ended in the middle of a value"),
Self::InvalidStart(b) => write!(f, "0x{b:02x} is not a frame start"),
Self::InvalidHeader => f.write_str("malformed long-frame header"),
Self::MissingStop => f.write_str("stop byte 0x16 missing"),
Self::Checksum { computed, found } => {
write!(f, "checksum 0x{found:02x} does not match computed 0x{computed:02x}")
}
Self::Crc { block, computed, found } => {
write!(f, "CRC of block {block} is 0x{found:04x}, computed 0x{computed:04x}")
}
Self::InvalidLength => f.write_str("length field inconsistent with the frame"),
Self::TooManyExtensions => f.write_str("more than 10 extension bytes"),
Self::ReservedDif(d) => write!(f, "reserved DIF 0x{d:02x}"),
Self::UnsupportedLvar(l) => write!(f, "LVAR 0x{l:02x} is reserved or unsupported"),
Self::UnsupportedCi(c) => write!(f, "CI field 0x{c:02x} is not supported yet"),
Self::OutputTooSmall => f.write_str("output buffer too small"),
Self::UnexpectedLayer(c) => {
write!(f, "CI 0x{c:02x} is not allowed at this point of the telegram")
}
Self::UnsupportedSecurity(m) => write!(f, "security mode {m} is not supported"),
Self::NoKey => f.write_str("no key for this meter"),
Self::UnknownFormat(s) => write!(f, "compact frame with unknown format signature 0x{s:04x}"),
Self::InvalidFormat => f.write_str("compact-frame layout with a special DIF or plain-text unit"),
Self::UnexpectedFragment { expected, found: 0 } => {
write!(f, "not a fragment (expected fragment {expected})")
}
Self::UnexpectedFragment { expected, found } => {
write!(f, "fragment {found} where fragment {expected} was expected")
}
Self::FragmentFromOtherMeter => f.write_str("fragment from another meter"),
Self::Verification(c) => write!(f, "verification failed: {c}"),
Self::InvalidAddress(a) => write!(f, "primary address {a} cannot be read"),
Self::InvalidBaud(n) => write!(f, "M-Bus has no baud rate {n} (300 to 38400)"),
}
}
}
impl core::error::Error for Error {}