pamoja_lorawan/error.rs
1//! The error type for LoRaWAN framing.
2
3/// What can go wrong building or reading a LoRaWAN frame.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum LorawanError {
6 /// A payload (or its frame options) is too large to fit a single frame.
7 PayloadTooLong,
8 /// A received frame is shorter than its fixed header and MIC require.
9 FrameTooShort,
10 /// A received frame's message type is not one this crate decodes here.
11 UnsupportedMType(u8),
12 /// A received frame's MIC does not match its contents, so it is forged or corrupt.
13 MicMismatch,
14 /// A received frame's counter does not match the counter expected for it.
15 FcntMismatch,
16 /// A received frame is structurally invalid in some other way.
17 MalformedFrame,
18 /// A MAC command identifier this version does not know. It cannot be stepped over,
19 /// because a command does not carry its own length.
20 UnknownCommand(u8),
21}
22
23impl core::fmt::Display for LorawanError {
24 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
25 match self {
26 LorawanError::PayloadTooLong => {
27 f.write_str("lorawan payload does not fit a single frame")
28 }
29 LorawanError::FrameTooShort => {
30 f.write_str("lorawan frame is shorter than its header and MIC")
31 }
32 LorawanError::UnsupportedMType(mtype) => {
33 write!(f, "lorawan message type {mtype:#04x} is not decoded here")
34 }
35 LorawanError::MicMismatch => f.write_str("lorawan MIC does not match the frame"),
36 LorawanError::FcntMismatch => {
37 f.write_str("lorawan frame counter does not match the one expected")
38 }
39 LorawanError::MalformedFrame => f.write_str("lorawan frame is malformed"),
40 LorawanError::UnknownCommand(cid) => {
41 write!(f, "lorawan MAC command {cid:#04x} is not known here")
42 }
43 }
44 }
45}
46
47// `core::error::Error` rather than `std::error::Error`, so a caller on a
48// microcontroller gets the same trait a caller on a gateway does.
49impl core::error::Error for LorawanError {}