1use core::fmt;
2
3pub type Result<T> = core::result::Result<T, Error>;
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12 UnexpectedEnd,
14 InvalidStart(u8),
16 InvalidHeader,
18 MissingStop,
20 Checksum {
22 computed: u8,
24 found: u8,
26 },
27 Crc {
29 block: usize,
31 computed: u16,
33 found: u16,
35 },
36 InvalidLength,
38 TooManyExtensions,
40 ReservedDif(u8),
42 UnsupportedLvar(u8),
44 UnsupportedCi(u8),
46 OutputTooSmall,
48 UnexpectedLayer(u8),
50 UnsupportedSecurity(u8),
52 NoKey,
54 UnknownFormat(u16),
57 InvalidFormat,
60 UnexpectedFragment {
63 expected: u8,
65 found: u8,
67 },
68 FragmentFromOtherMeter,
70 Verification(Check),
72 InvalidAddress(u8),
75}
76
77#[derive(Clone, Copy, Debug, PartialEq, Eq)]
79#[non_exhaustive]
80pub enum Check {
81 Marker,
83 Mac,
86 PayloadCrc,
88 Length,
91 MissingCounter,
93 FullFrameCrc,
95 Tag,
97}
98
99impl fmt::Display for Check {
100 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101 f.write_str(match self {
102 Self::Marker => "decrypted payload does not start with 2F 2F",
103 Self::Mac => "message authentication code mismatch",
104 Self::PayloadCrc => "ELL payload CRC mismatch",
105 Self::Length => "encrypted length is not a whole number of blocks, or longer than the data",
106 Self::MissingCounter => "mode 7 without AFL message counter",
107 Self::FullFrameCrc => "compact frame does not match its full-frame CRC",
108 Self::Tag => "AES-CCM tag mismatch",
109 })
110 }
111}
112
113impl fmt::Display for Error {
114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 match self {
116 Self::UnexpectedEnd => f.write_str("input ended in the middle of a value"),
117 Self::InvalidStart(b) => write!(f, "0x{b:02x} is not a frame start"),
118 Self::InvalidHeader => f.write_str("malformed long-frame header"),
119 Self::MissingStop => f.write_str("stop byte 0x16 missing"),
120 Self::Checksum { computed, found } => {
121 write!(f, "checksum 0x{found:02x} does not match computed 0x{computed:02x}")
122 }
123 Self::Crc { block, computed, found } => {
124 write!(f, "CRC of block {block} is 0x{found:04x}, computed 0x{computed:04x}")
125 }
126 Self::InvalidLength => f.write_str("length field inconsistent with the frame"),
127 Self::TooManyExtensions => f.write_str("more than 10 extension bytes"),
128 Self::ReservedDif(d) => write!(f, "reserved DIF 0x{d:02x}"),
129 Self::UnsupportedLvar(l) => write!(f, "LVAR 0x{l:02x} is reserved or unsupported"),
130 Self::UnsupportedCi(c) => write!(f, "CI field 0x{c:02x} is not supported yet"),
131 Self::OutputTooSmall => f.write_str("output buffer too small"),
132 Self::UnexpectedLayer(c) => {
133 write!(f, "CI 0x{c:02x} is not allowed at this point of the telegram")
134 }
135 Self::UnsupportedSecurity(m) => write!(f, "security mode {m} is not supported"),
136 Self::NoKey => f.write_str("no key for this meter"),
137 Self::UnknownFormat(s) => write!(f, "compact frame with unknown format signature 0x{s:04x}"),
138 Self::InvalidFormat => f.write_str("compact-frame layout with a special DIF or plain-text unit"),
139 Self::UnexpectedFragment { expected, found: 0 } => {
140 write!(f, "not a fragment (expected fragment {expected})")
141 }
142 Self::UnexpectedFragment { expected, found } => {
143 write!(f, "fragment {found} where fragment {expected} was expected")
144 }
145 Self::FragmentFromOtherMeter => f.write_str("fragment from another meter"),
146 Self::Verification(c) => write!(f, "verification failed: {c}"),
147 Self::InvalidAddress(a) => write!(f, "no meter answers from primary address {a}"),
148 }
149 }
150}
151
152#[cfg(feature = "std")]
153impl std::error::Error for Error {}