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}
73
74#[derive(Clone, Copy, Debug, PartialEq, Eq)]
76#[non_exhaustive]
77pub enum Check {
78 Marker,
80 Mac,
83 PayloadCrc,
85 Length,
88 MissingCounter,
90 FullFrameCrc,
92 Tag,
94}
95
96impl fmt::Display for Check {
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 f.write_str(match self {
99 Self::Marker => "decrypted payload does not start with 2F 2F",
100 Self::Mac => "message authentication code mismatch",
101 Self::PayloadCrc => "ELL payload CRC mismatch",
102 Self::Length => "encrypted length is not a whole number of blocks, or longer than the data",
103 Self::MissingCounter => "mode 7 without AFL message counter",
104 Self::FullFrameCrc => "compact frame does not match its full-frame CRC",
105 Self::Tag => "AES-CCM tag mismatch",
106 })
107 }
108}
109
110impl fmt::Display for Error {
111 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112 match self {
113 Self::UnexpectedEnd => f.write_str("input ended in the middle of a value"),
114 Self::InvalidStart(b) => write!(f, "0x{b:02x} is not a frame start"),
115 Self::InvalidHeader => f.write_str("malformed long-frame header"),
116 Self::MissingStop => f.write_str("stop byte 0x16 missing"),
117 Self::Checksum { computed, found } => {
118 write!(f, "checksum 0x{found:02x} does not match computed 0x{computed:02x}")
119 }
120 Self::Crc { block, computed, found } => {
121 write!(f, "CRC of block {block} is 0x{found:04x}, computed 0x{computed:04x}")
122 }
123 Self::InvalidLength => f.write_str("length field inconsistent with the frame"),
124 Self::TooManyExtensions => f.write_str("more than 10 extension bytes"),
125 Self::ReservedDif(d) => write!(f, "reserved DIF 0x{d:02x}"),
126 Self::UnsupportedLvar(l) => write!(f, "LVAR 0x{l:02x} is reserved or unsupported"),
127 Self::UnsupportedCi(c) => write!(f, "CI field 0x{c:02x} is not supported yet"),
128 Self::OutputTooSmall => f.write_str("output buffer too small"),
129 Self::UnexpectedLayer(c) => {
130 write!(f, "CI 0x{c:02x} is not allowed at this point of the telegram")
131 }
132 Self::UnsupportedSecurity(m) => write!(f, "security mode {m} is not supported"),
133 Self::NoKey => f.write_str("no key for this meter"),
134 Self::UnknownFormat(s) => write!(f, "compact frame with unknown format signature 0x{s:04x}"),
135 Self::InvalidFormat => f.write_str("compact-frame layout with a special DIF or plain-text unit"),
136 Self::UnexpectedFragment { expected, found: 0 } => {
137 write!(f, "not a fragment (expected fragment {expected})")
138 }
139 Self::UnexpectedFragment { expected, found } => {
140 write!(f, "fragment {found} where fragment {expected} was expected")
141 }
142 Self::FragmentFromOtherMeter => f.write_str("fragment from another meter"),
143 Self::Verification(c) => write!(f, "verification failed: {c}"),
144 }
145 }
146}
147
148#[cfg(feature = "std")]
149impl std::error::Error for Error {}