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 root_wor_s_key(&self) -> [u8; 16]
pub fn root_wor_s_key(&self) -> [u8; 16]
Returns the root relay session key of the device, TS011-1.0.1 section 4.4.
It comes from the network session key, as LoRaWAN 1.0.x has it, and is what a network
sends a relay in UpdateUplinkListReq so the relay can verify this device.
§Returns
The key.
Sourcepub fn wor_keys(&self) -> WorKeys
pub fn wor_keys(&self) -> WorKeys
Returns the keys the device’s wake-on-radio frames are protected with, TS011-1.0.1 section 4.5.
§Returns
The integrity and encryption keys, derived from
root_wor_s_key and the device address.
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, and LorawanError::MalformedFrame for port 0 with frame options,
which would carry MAC commands in both places at once.
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, and LorawanError::MalformedFrame for port 0 with frame options.
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,
LorawanError::MicMismatch if the MIC does not verify, or
LorawanError::MalformedFrame if an authentic frame carries frame options on port
0, which TS001-1.0.4 section 4.3.1.6 has a device discard.