Skip to main content

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}
19
20impl core::fmt::Display for LorawanError {
21    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22        match self {
23            LorawanError::PayloadTooLong => {
24                f.write_str("lorawan payload does not fit a single frame")
25            }
26            LorawanError::FrameTooShort => {
27                f.write_str("lorawan frame is shorter than its header and MIC")
28            }
29            LorawanError::UnsupportedMType(mtype) => {
30                write!(f, "lorawan message type {mtype:#04x} is not decoded here")
31            }
32            LorawanError::MicMismatch => f.write_str("lorawan MIC does not match the frame"),
33            LorawanError::FcntMismatch => {
34                f.write_str("lorawan frame counter does not match the one expected")
35            }
36            LorawanError::MalformedFrame => f.write_str("lorawan frame is malformed"),
37        }
38    }
39}
40
41// `core::error::Error` rather than `std::error::Error`, so a caller on a
42// microcontroller gets the same trait a caller on a gateway does.
43impl core::error::Error for LorawanError {}