pub struct Session { /* private fields */ }Expand description
An activated LoRaWAN session: a device address and the two session keys.
This is the state a device holds once it is activated, whether by personalization (the address and keys provisioned directly) or by a join exchange. It secures every data frame: the network session key authenticates the whole frame through its MIC, and the application session key encrypts the payload, with the device address and frame counter folded into both so a frame is bound to its place in the stream.
§Examples
use pamoja_lorawan::{Session, Uplink};
let session = Session::new(0x2601_1BDA, [0x11; 16], [0x22; 16]);
let frame = session.encode_uplink(&Uplink::new(1, 1, b"hello")).unwrap();
// The receiver, holding the same session, recovers the payload.
let rx = session.decode(frame.as_bytes(), 1).unwrap();
assert_eq!(rx.payload(), b"hello");Implementations§
Source§impl Session
impl Session
Sourcepub fn encode_uplink(
&self,
uplink: &Uplink<'_>,
) -> Result<PhyPayload, LorawanError>
pub fn encode_uplink( &self, uplink: &Uplink<'_>, ) -> Result<PhyPayload, LorawanError>
Encodes an uplink data frame, encrypting the payload and appending the MIC.
§Arguments
uplink- the uplink to send.
§Returns
The frame ready for the radio.
§Errors
Returns LorawanError::PayloadTooLong if the payload and options do not fit a
single frame.
Sourcepub fn encode_downlink(
&self,
downlink: &Downlink<'_>,
) -> Result<PhyPayload, LorawanError>
pub fn encode_downlink( &self, downlink: &Downlink<'_>, ) -> Result<PhyPayload, LorawanError>
Encodes a downlink data frame, encrypting the payload and appending the MIC.
§Arguments
downlink- the downlink to send.
§Returns
The frame ready for the radio.
§Errors
Returns LorawanError::PayloadTooLong if the payload and options do not fit a
single frame.
Sourcepub fn decode(&self, bytes: &[u8], fcnt: u32) -> Result<RxData, LorawanError>
pub fn decode(&self, bytes: &[u8], fcnt: u32) -> Result<RxData, LorawanError>
Decodes a received data frame: verifies the MIC, then decrypts the payload.
§Arguments
bytes- the raw frame as it came off the radio.fcnt- the full 32-bit frame counter expected for this frame; its low 16 bits must match the counter the frame carries.
§Returns
The decoded frame, with its payload decrypted.
§Errors
Returns LorawanError::FrameTooShort if the frame is too small,
LorawanError::UnsupportedMType if it is not a data frame,
LorawanError::FcntMismatch if the counter does not match, or
LorawanError::MicMismatch if the MIC does not verify.