1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#![no_std]
#![cfg_attr(feature = "async", feature(async_fn_in_trait))]
#![allow(incomplete_features)]

use heapless::Vec;

pub mod radio;

mod mac;
use mac::Mac;

mod types;
pub use types::*;

pub mod region;
pub use region::Region;

mod state_machines;
use core::marker::PhantomData;
use lorawan::{
    keys::{CryptoFactory, AES128},
    parser::{DecryptedDataPayload, DevAddr},
};
use state_machines::Shared;
pub use state_machines::{no_session, no_session::SessionData, session};

pub use rand_core::RngCore;

#[cfg(feature = "async")]
pub mod async_device;

type TimestampMs = u32;

pub struct Device<R, C, RNG, const N: usize>
where
    R: radio::PhyRxTx + Timings,
    C: CryptoFactory + Default,
    RNG: RngCore,
{
    state: Option<State>,
    shared: Shared<R, RNG, N>,
    crypto: PhantomData<C>,
}

type FcntDown = u32;
type FcntUp = u32;

#[derive(Debug)]
pub enum Response {
    NoUpdate,
    TimeoutRequest(TimestampMs),
    JoinRequestSending,
    JoinSuccess,
    NoJoinAccept,
    UplinkSending(FcntUp),
    DownlinkReceived(FcntDown),
    NoAck,
    ReadyToSend,
    SessionExpired,
}

#[derive(Debug)]
pub enum Error<R> {
    Radio(radio::Error<R>),
    Session(session::Error),
    NoSession(no_session::Error),
}

impl<R> From<radio::Error<R>> for Error<R> {
    fn from(radio_error: radio::Error<R>) -> Error<R> {
        Error::Radio(radio_error)
    }
}

pub enum Event<'a, R>
where
    R: radio::PhyRxTx,
{
    NewSessionRequest,
    SendDataRequest(SendData<'a>),
    RadioEvent(radio::Event<'a, R>),
    TimeoutFired,
}

impl<'a, R> core::fmt::Debug for Event<'a, R>
where
    R: radio::PhyRxTx,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let event = match self {
            Event::NewSessionRequest => "NewSessionRequest",
            Event::SendDataRequest(_) => "SendDataRequest",
            Event::RadioEvent(_) => "RadioEvent(?)",
            Event::TimeoutFired => "TimeoutFired",
        };
        write!(f, "lorawan_device::Event::{event}")
    }
}

pub struct SendData<'a> {
    data: &'a [u8],
    fport: u8,
    confirmed: bool,
}

pub enum State {
    NoSession(no_session::NoSession),
    Session(session::Session),
}

use core::default::Default;
impl State {
    fn new() -> Self {
        State::NoSession(no_session::NoSession::new())
    }

    fn new_abp(newskey: AES128, appskey: AES128, devaddr: DevAddr<[u8; 4]>) -> Self {
        let session_data = SessionData::new(newskey, appskey, devaddr);
        State::Session(session::Session::new(session_data))
    }
}

pub trait Timings {
    fn get_rx_window_offset_ms(&self) -> i32;
    fn get_rx_window_duration_ms(&self) -> u32;
}

pub enum JoinMode {
    OTAA {
        deveui: [u8; 8],
        appeui: [u8; 8],
        appkey: [u8; 16],
    },
    ABP {
        newskey: AES128,
        appskey: AES128,
        devaddr: DevAddr<[u8; 4]>,
    },
}

#[allow(dead_code)]
impl<R, C, RNG, const N: usize> Device<R, C, RNG, N>
where
    R: radio::PhyRxTx + Timings,
    C: CryptoFactory + Default,
    RNG: RngCore,
{
    pub fn new(
        region: region::Configuration,
        join_mode: JoinMode,
        radio: R,
        rng: RNG,
    ) -> Device<R, C, RNG, N> {
        let (shared, state) = match join_mode {
            JoinMode::OTAA {
                deveui,
                appeui,
                appkey,
            } => (
                Shared::new(
                    radio,
                    Some(Credentials::new(appeui, deveui, appkey)),
                    region,
                    Mac::default(),
                    rng,
                ),
                State::new(),
            ),
            JoinMode::ABP {
                newskey,
                appskey,
                devaddr,
            } => (
                Shared::new(radio, None, region, Mac::default(), rng),
                State::new_abp(newskey, appskey, devaddr),
            ),
        };

        Device {
            crypto: PhantomData::default(),
            shared,
            state: Some(state),
        }
    }

    pub fn get_radio(&mut self) -> &mut R {
        let shared = self.get_shared();
        shared.get_mut_radio()
    }

    pub fn get_credentials(&mut self) -> &mut Option<Credentials> {
        let shared = self.get_shared();
        shared.get_mut_credentials()
    }

    fn get_shared(&mut self) -> &mut Shared<R, RNG, N> {
        &mut self.shared
    }

    pub fn get_datarate(&mut self) -> region::DR {
        self.get_shared().get_datarate()
    }

    pub fn set_datarate(&mut self, datarate: region::DR) {
        self.get_shared().set_datarate(datarate);
    }

    pub fn ready_to_send_data(&self) -> bool {
        matches!(
            &self.state.as_ref().unwrap(),
            State::Session(session::Session::Idle(_))
        )
    }

    pub fn send(
        &mut self,
        data: &[u8],
        fport: u8,
        confirmed: bool,
    ) -> Result<Response, Error<R::PhyError>> {
        self.handle_event(Event::SendDataRequest(SendData {
            data,
            fport,
            confirmed,
        }))
    }

    pub fn get_fcnt_up(&self) -> Option<u32> {
        if let State::Session(session) = &self.state.as_ref().unwrap() {
            Some(session.get_session_data().fcnt_up())
        } else {
            None
        }
    }

    pub fn get_session_keys(&self) -> Option<SessionKeys> {
        if let State::Session(session) = &self.state.as_ref().unwrap() {
            Some(SessionKeys::copy_from_session_data(
                session.get_session_data(),
            ))
        } else {
            None
        }
    }

    pub fn take_data_downlink(&mut self) -> Option<DecryptedDataPayload<Vec<u8, 256>>> {
        self.get_shared().take_data_downlink()
    }

    pub fn handle_event(&mut self, event: Event<R>) -> Result<Response, Error<R::PhyError>> {
        let (new_state, result) = match self.state.take().unwrap() {
            State::NoSession(state) => state.handle_event::<R, C, RNG, N>(event, &mut self.shared),
            State::Session(state) => state.handle_event::<R, C, RNG, N>(event, &mut self.shared),
        };
        self.state.replace(new_state);
        result
    }
}

/// Trait used to mark types which can give out an exclusice reference to [`RngCore`].
/// This trait is an implementation detail and should not be implemented outside this crate.
#[doc(hidden)]
pub trait GetRng: private::Sealed {
    type RNG: RngCore;
    fn get_rng(&mut self) -> &mut Self::RNG;
}

mod private {
    /// Super trait used to mark traits with an exhaustive set of
    /// implementations
    pub trait Sealed {}
}