Skip to main content

Session

Struct Session 

Source
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

Source

pub fn new(dev_addr: u32, nwk_skey: [u8; 16], app_skey: [u8; 16]) -> Self

Creates a session from a device address and its two session keys.

§Arguments
  • dev_addr - the device address the network assigned.
  • nwk_skey - the network session key, which authenticates frames.
  • app_skey - the application session key, which encrypts payloads.
§Returns

The session.

Source

pub fn dev_addr(&self) -> u32

Returns the device address this session is bound to.

§Returns

The device address.

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.

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Session

Source§

fn clone(&self) -> Session

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Session

Source§

impl Debug for Session

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Session

Source§

impl PartialEq for Session

Source§

fn eq(&self, other: &Session) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Session

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.