semtech-udp 0.12.0

Semtech UDP provides serialization and deserialization of packets complying with the Semtech UDP protocol
Documentation
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
use super::{
    pull_resp, pull_resp::TxPk, tx_ack::Packet as TxAck, MacAddress, Packet, ParseError,
    SerializablePacket, Up,
};
pub use crate::push_data::{RxPk, Stat};
use std::sync::Arc;
use std::time::SystemTime;
use std::{collections::HashMap, net::SocketAddr, time::Duration};
use tokio::{
    net::{ToSocketAddrs, UdpSocket},
    sync::{mpsc, oneshot},
    time::timeout,
};

mod error;
pub use error::Error;
pub type Result<T = ()> = std::result::Result<T, Error>;

const DEFAULT_DISCONNECT_THRESHOLD: u64 = 60;
const DEFAULT_CACHE_CHECK_FREQ: u64 = 60;
const MAX_MESSAGE_SIZE: usize = 65535;

#[derive(Debug)]
enum InternalEvent {
    Downlink((pull_resp::Packet, MacAddress, oneshot::Sender<TxAck>)),
    PacketBySocket((Packet, SocketAddr)),
    Client((MacAddress, SocketAddr)),
    PacketReceived(RxPk, MacAddress),
    StatReceived(Stat, MacAddress),
    UnableToParseUdpFrame(ParseError, Vec<u8>),
    AckReceived(TxAck),
    CheckCache,
    FailedSend((Box<pull_resp::Packet>, MacAddress)),
    SuccessSend((u16, oneshot::Sender<TxAck>)),
}

#[derive(Debug)]
pub enum Event {
    PacketReceived(RxPk, MacAddress),
    StatReceived(Stat, MacAddress),
    NewClient((MacAddress, SocketAddr)),
    UpdateClient((MacAddress, SocketAddr)),
    UnableToParseUdpFrame(ParseError, Vec<u8>),
    NoClientWithMac(Box<pull_resp::Packet>, MacAddress),
    ClientDisconnected((MacAddress, SocketAddr)),
}

// receives requests from clients
// dispatches them to UdpTx
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct ClientTx {
    sender: mpsc::Sender<InternalEvent>,
}

// sends packets to clients
#[derive(Debug)]
pub struct ClientRx {
    receiver: mpsc::Receiver<Event>,
}

// receives and parses UDP packets
struct UdpRx {
    socket_receiver: Arc<UdpSocket>,
    internal_sender: mpsc::Sender<InternalEvent>,
}

// processes Internal Events and Transmit over UDP
struct Internal {
    self_sender: mpsc::Sender<InternalEvent>,
    receiver: mpsc::Receiver<InternalEvent>,
    client_tx_sender: mpsc::Sender<Event>,
    clients: HashMap<MacAddress, Client>,
    downlink_senders: HashMap<u16, oneshot::Sender<TxAck>>,
    socket_sender: Arc<UdpSocket>,
    disconnect_threshold: Option<Duration>,
}

#[derive(Debug, Clone)]
struct Client {
    addr: SocketAddr,
    last_seen: SystemTime,
}

impl Client {
    fn new(addr: SocketAddr) -> Self {
        Client {
            addr,
            last_seen: SystemTime::now(),
        }
    }
    fn addr(&self) -> &SocketAddr {
        &self.addr
    }

    fn update_addr(&mut self, new_addr: SocketAddr) {
        self.addr = new_addr;
        self.seen();
    }

    fn seen(&mut self) {
        self.last_seen = SystemTime::now();
    }
}

#[derive(Debug)]
pub struct UdpRuntime {
    rx: ClientRx,
    tx: ClientTx,
}
use rand::Rng;

#[derive(Clone)]
pub struct Downlink {
    mac: MacAddress,
    packet: Option<pull_resp::Packet>,
    sender: mpsc::Sender<InternalEvent>,
}

impl Downlink {
    pub fn set_packet(&mut self, txpk: TxPk) {
        self.packet = Some(pull_resp::Packet {
            random_token: rand::thread_rng().gen(),
            data: pull_resp::Data::from_txpk(txpk),
        });
    }

    pub fn get_destination_mac(&mut self) -> MacAddress {
        self.mac
    }

    async fn just_dispatch(self) -> Result<Option<u32>> {
        if let Some(packet) = self.packet {
            let (sender, receiver) = oneshot::channel();

            self.sender
                .send(InternalEvent::Downlink((packet, self.mac, sender)))
                .await?;

            // wait for the ACK for the protocol layer
            receiver.await?.get_result().map_err(|e| e.into())
        } else {
            Err(Error::DispatchWithNoSendPacket)
        }
    }

    pub async fn dispatch(self, timeout_duration: Option<Duration>) -> Result<Option<u32>> {
        if let Some(duration) = timeout_duration {
            timeout(duration, self.just_dispatch()).await?
        } else {
            self.just_dispatch().await
        }
    }
}

impl ClientRx {
    pub async fn recv(&mut self) -> Event {
        // we unwrap here because the send channel is dropped only iff ClientRx is dropped
        // ClientRx panics before it can get dropped (see UdpRuntime)
        self.receiver.recv().await.unwrap()
    }
}

impl ClientTx {
    pub async fn send(
        &mut self,
        txpk: TxPk,
        mac: MacAddress,
        timeout: Option<Duration>,
    ) -> Result<Option<u32>> {
        let prepared_send = self.prepare_downlink(Some(txpk), mac);
        prepared_send.dispatch(timeout).await
    }

    pub fn prepare_downlink(&mut self, txpk: Option<TxPk>, mac: MacAddress) -> Downlink {
        let packet = txpk.map(|txpk| pull_resp::Packet {
            random_token: rand::thread_rng().gen(),
            data: pull_resp::Data::from_txpk(txpk),
        });

        Downlink {
            mac,
            packet,
            sender: self.get_sender(),
        }
    }

    fn get_sender(&mut self) -> mpsc::Sender<InternalEvent> {
        self.sender.clone()
    }
}

impl UdpRuntime {
    pub fn split(self) -> (ClientRx, ClientTx) {
        (self.rx, self.tx)
    }

    pub async fn send(
        &mut self,
        txpk: TxPk,
        mac: MacAddress,
        timeout: Option<Duration>,
    ) -> Result<Option<u32>> {
        self.tx.send(txpk, mac, timeout).await
    }

    pub fn prepare_empty_downlink(&mut self, mac: MacAddress) -> Downlink {
        self.tx.prepare_downlink(None, mac)
    }

    pub fn prepare_downlink(&mut self, txpk: TxPk, mac: MacAddress) -> Downlink {
        self.tx.prepare_downlink(Some(txpk), mac)
    }

    pub async fn recv(&mut self) -> Event {
        self.rx.recv().await
    }

    pub async fn new<A: ToSocketAddrs>(addr: A) -> Result<UdpRuntime> {
        let socket = UdpSocket::bind(&addr).await?;
        let socket_receiver = Arc::new(socket);
        let socket_sender = socket_receiver.clone();

        let (udp_tx_sender, udp_tx_receiver) = mpsc::channel(100);
        let (client_tx_sender, client_tx_receiver) = mpsc::channel(100);

        let client_tx = ClientTx {
            sender: udp_tx_sender.clone(),
        };

        let client_rx = ClientRx {
            receiver: client_tx_receiver,
        };

        let udp_rx = UdpRx {
            socket_receiver,
            internal_sender: udp_tx_sender.clone(),
        };

        let udp_tx = Internal {
            self_sender: udp_tx_sender,
            receiver: udp_tx_receiver,
            client_tx_sender,
            clients: HashMap::new(),
            downlink_senders: HashMap::new(),
            socket_sender,
            disconnect_threshold: Some(Duration::from_secs(DEFAULT_DISCONNECT_THRESHOLD)),
        };

        // udp_rx reads from the UDP port
        // and sends packets to relevant parties
        tokio::spawn(async move {
            if let Err(e) = udp_rx.run().await {
                // we panic here because the ony error case here
                // if we lost the local socket somehow
                panic!("UdpRx threw error: {e:?}")
            }
        });

        // udp_tx writes to the UDP port and maintains
        // gateway to IP map
        tokio::spawn(async move {
            if let Err(e) = udp_tx.run().await {
                // we panic here because the ony error case here
                // if we lost the local socket somehow
                panic!("UdpTx threw error: {e:?}")
            }
        });

        Ok(UdpRuntime {
            rx: client_rx,
            tx: client_tx,
        })
    }
}

impl UdpRx {
    pub async fn run(self) -> Result {
        let cache_sender = self.internal_sender.clone();
        let cache_sender = tokio::spawn(async move {
            loop {
                cache_sender.send(InternalEvent::CheckCache).await?;
                tokio::time::sleep(Duration::from_secs(DEFAULT_CACHE_CHECK_FREQ)).await;
            }
        });

        let socket_handler = tokio::spawn(async move {
            let mut buf = vec![0u8; MAX_MESSAGE_SIZE];
            loop {
                match self.socket_receiver.recv_from(&mut buf).await {
                    Err(e) => return Err(e.into()),
                    Ok((n, src)) => {
                        let packet = match Packet::parse_uplink(&buf[0..n]) {
                            Ok(packet) => Some(packet),
                            Err(e) => {
                                let mut vec = Vec::new();
                                vec.extend_from_slice(&buf[0..n]);
                                self.internal_sender
                                    .send(InternalEvent::UnableToParseUdpFrame(e, vec))
                                    .await?;
                                None
                            }
                        };
                        if let Some(packet) = packet {
                            match packet {
                                Up::PullData(pull_data) => {
                                    let mac = pull_data.gateway_mac;
                                    // first send (mac, addr) to update map owned by UdpRuntimeTx
                                    let client = (mac, src);
                                    self.internal_sender
                                        .send(InternalEvent::Client(client))
                                        .await?;

                                    // send the ack_packet
                                    let ack_packet = pull_data.into_ack();
                                    self.internal_sender
                                        .send(InternalEvent::PacketBySocket((
                                            ack_packet.into(),
                                            src,
                                        )))
                                        .await?
                                }
                                Up::TxAck(txack) => {
                                    self.internal_sender
                                        .send(InternalEvent::AckReceived(txack))
                                        .await?;
                                }
                                Up::PushData(push_data) => {
                                    // Send all received packets as RxPk Events
                                    if let Some(rxpk) = &push_data.data.rxpk {
                                        for packet in rxpk {
                                            self.internal_sender
                                                .send(InternalEvent::PacketReceived(
                                                    packet.clone(),
                                                    push_data.gateway_mac,
                                                ))
                                                .await?;
                                        }
                                    }

                                    if let Some(stat) = &push_data.data.stat {
                                        self.internal_sender
                                            .send(InternalEvent::StatReceived(
                                                stat.clone(),
                                                push_data.gateway_mac,
                                            ))
                                            .await?;
                                    }

                                    let socket_addr = src;
                                    // send the ack_packet
                                    let ack_packet = push_data.into_ack();
                                    self.internal_sender
                                        .send(InternalEvent::PacketBySocket((
                                            ack_packet.into(),
                                            socket_addr,
                                        )))
                                        .await?;
                                }
                            }
                        }
                    }
                }
            }
        });

        tokio::select!(
            resp = cache_sender => resp?,
            resp = socket_handler => resp?,
        )
    }
}

impl Internal {
    pub async fn run(mut self) -> Result {
        let mut buf = vec![0u8; MAX_MESSAGE_SIZE];
        loop {
            let msg = self.receiver.recv().await;
            if let Some(msg) = msg {
                match msg {
                    InternalEvent::CheckCache => {
                        let now = SystemTime::now();
                        if let Some(disconnect_threshold) = self.disconnect_threshold {
                            for (mac, client) in self.clients.clone().into_iter() {
                                let time_since_last_seen = now
                                    .duration_since(client.last_seen)
                                    .map_err(|_| Error::LastSeen {
                                        last_seen: client.last_seen,
                                        now,
                                    })?;

                                if time_since_last_seen > disconnect_threshold {
                                    // Client not connected
                                    self.client_tx_sender
                                        .send(Event::ClientDisconnected((mac, *client.addr())))
                                        .await?;
                                    self.clients.remove(&mac);
                                }
                            }
                        }
                    }
                    InternalEvent::UnableToParseUdpFrame(error, frame) => {
                        self.client_tx_sender
                            .send(Event::UnableToParseUdpFrame(error, frame))
                            .await?;
                    }
                    InternalEvent::PacketReceived(rxpk, mac) => {
                        self.client_tx_sender
                            .send(Event::PacketReceived(rxpk, mac))
                            .await?;
                    }
                    InternalEvent::StatReceived(stat, mac) => {
                        self.client_tx_sender
                            .send(Event::StatReceived(stat, mac))
                            .await?;
                    }
                    InternalEvent::Downlink((packet, mac, ack_sender)) => {
                        if let Some(client) = self.clients.get(&mac) {
                            // we spawn off here because one slow client can slow down all of the
                            // event processing
                            let n = packet.serialize(&mut buf)? as usize;
                            let buf = Vec::from(&buf[..n]);
                            let socket_sender = self.socket_sender.clone();
                            let client_addr = *client.addr();
                            let self_sender = self.self_sender.clone();
                            tokio::spawn(async move {
                                match socket_sender.send_to(&buf, client_addr).await {
                                    Err(_) => {
                                        self_sender
                                            .send(InternalEvent::FailedSend((packet.into(), mac)))
                                            .await
                                            .unwrap();
                                    }
                                    Ok(_) => {
                                        self_sender
                                            .send(InternalEvent::SuccessSend((
                                                packet.random_token,
                                                ack_sender,
                                            )))
                                            .await
                                            .unwrap();
                                    }
                                }
                            });
                        } else {
                            self.client_tx_sender
                                .send(Event::NoClientWithMac(packet.into(), mac))
                                .await?;
                        }
                    }
                    InternalEvent::AckReceived(txack) => {
                        if let Some(sender) = self.downlink_senders.remove(&txack.random_token) {
                            // we may have received an ACK on a transmit that timed out already
                            // therefore, this send may fail.
                            let _ = sender.send(txack);
                        }
                    }
                    InternalEvent::PacketBySocket((packet, addr)) => {
                        let n = packet.serialize(&mut buf)? as usize;
                        // only ACKs are sent via PacketBySocket
                        // so this will be an error only if we have somehow lost UDP connection
                        // between receiving a packet and sending the ACK
                        let _ = self.socket_sender.send_to(&buf[..n], &addr).await;
                    }
                    InternalEvent::Client((mac, addr)) => {
                        // tell user if same MAC has new IP
                        if let Some(client) = self.clients.get_mut(&mac) {
                            if *client.addr() != addr {
                                client.update_addr(addr);
                                self.client_tx_sender
                                    .send(Event::UpdateClient((mac, addr)))
                                    .await?;
                            } else {
                                // refresh the seen
                                client.seen();
                            }
                        }
                        // simply insert if no entry exists
                        else {
                            self.clients.insert(mac, Client::new(addr));
                            self.client_tx_sender
                                .send(Event::NewClient((mac, addr)))
                                .await?;
                        }
                    }
                    InternalEvent::SuccessSend((random_token, ack_sender)) => {
                        self.downlink_senders.insert(random_token, ack_sender);
                    }
                    InternalEvent::FailedSend((packet, mac)) => {
                        self.clients.remove(&mac);
                        self.client_tx_sender
                            .send(Event::NoClientWithMac(packet, mac))
                            .await?;
                    }
                }
            }
        }
    }
}