rns-net 0.5.5

Network interfaces and node driver for the Reticulum Network Stack
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! RNode-specific KISS protocol commands and streaming decoder.
//!
//! Extends `kiss.rs` with RNode command constants, multi-byte responses,
//! and subinterface routing for multi-radio RNode devices.
//! Matches Python `RNodeInterface.py` and `RNodeMultiInterface.py`.

use crate::kiss;

// ── RNode KISS command bytes ────────────────────────────────────────────

pub const CMD_FREQUENCY: u8 = 0x01;
pub const CMD_BANDWIDTH: u8 = 0x02;
pub const CMD_TXPOWER: u8 = 0x03;
pub const CMD_SF: u8 = 0x04;
pub const CMD_CR: u8 = 0x05;
pub const CMD_RADIO_STATE: u8 = 0x06;
pub const CMD_RADIO_LOCK: u8 = 0x07;
pub const CMD_DETECT: u8 = 0x08;
pub const CMD_LEAVE: u8 = 0x0A;
pub const CMD_ST_ALOCK: u8 = 0x0B;
pub const CMD_LT_ALOCK: u8 = 0x0C;
pub const CMD_READY: u8 = 0x0F;
pub const CMD_SEL_INT: u8 = 0x1F;
pub const CMD_STAT_RSSI: u8 = 0x23;
pub const CMD_STAT_SNR: u8 = 0x24;
pub const CMD_RANDOM: u8 = 0x40;
pub const CMD_PLATFORM: u8 = 0x48;
pub const CMD_MCU: u8 = 0x49;
pub const CMD_FW_VERSION: u8 = 0x50;
pub const CMD_FW_DETAIL: u8 = 0x51;
pub const CMD_RESET: u8 = 0x55;
pub const CMD_INTERFACES: u8 = 0x71;
pub const CMD_ERROR: u8 = 0x90;

pub const DETECT_REQ: u8 = 0x73;
pub const DETECT_RESP: u8 = 0x46;

pub const RADIO_STATE_OFF: u8 = 0x00;
pub const RADIO_STATE_ON: u8 = 0x01;

// Subinterface data command bytes (from RNodeMultiInterface.py)
const CMD_INT0_DATA: u8 = 0x00;
const CMD_INT1_DATA: u8 = 0x10;
const CMD_INT2_DATA: u8 = 0x20;
const CMD_INT3_DATA: u8 = 0x70;
const CMD_INT4_DATA: u8 = 0x75;
const CMD_INT5_DATA: u8 = 0x90;
const CMD_INT6_DATA: u8 = 0xA0;
const CMD_INT7_DATA: u8 = 0xB0;
const CMD_INT8_DATA: u8 = 0xC0;
const CMD_INT9_DATA: u8 = 0xD0;
const CMD_INT10_DATA: u8 = 0xE0;
const CMD_INT11_DATA: u8 = 0xF0;

/// All subinterface data command bytes, indexed by subinterface number.
const DATA_CMDS: [u8; 12] = [
    CMD_INT0_DATA,
    CMD_INT1_DATA,
    CMD_INT2_DATA,
    CMD_INT3_DATA,
    CMD_INT4_DATA,
    CMD_INT5_DATA,
    CMD_INT6_DATA,
    CMD_INT7_DATA,
    CMD_INT8_DATA,
    CMD_INT9_DATA,
    CMD_INT10_DATA,
    CMD_INT11_DATA,
];

/// Map a command byte to a subinterface data index, or None.
fn data_cmd_to_index(cmd: u8) -> Option<usize> {
    DATA_CMDS.iter().position(|&c| c == cmd)
}

// ── Events ──────────────────────────────────────────────────────────────

/// Events yielded by the RNode decoder.
#[derive(Debug, Clone, PartialEq)]
pub enum RNodeEvent {
    /// A data frame was received on the given subinterface.
    DataFrame { index: usize, data: Vec<u8> },
    /// Device detection response.
    Detected(bool),
    /// Firmware version reported.
    FirmwareVersion { major: u8, minor: u8 },
    /// Platform byte reported.
    Platform(u8),
    /// MCU byte reported.
    Mcu(u8),
    /// Interface type for a given index.
    InterfaceType { index: u8, type_byte: u8 },
    /// Reported frequency (Hz).
    Frequency(u32),
    /// Reported bandwidth (Hz).
    Bandwidth(u32),
    /// Reported TX power (dBm, signed).
    TxPower(i8),
    /// Reported spreading factor.
    SpreadingFactor(u8),
    /// Reported coding rate.
    CodingRate(u8),
    /// Reported radio state.
    RadioState(u8),
    /// Reported RSSI (raw byte, caller subtracts RSSI_OFFSET=157).
    StatRssi(u8),
    /// Reported SNR (signed, multiply by 0.25 for dB).
    StatSnr(i8),
    /// Reported short-term airtime lock (percent * 100).
    StAlock(u16),
    /// Reported long-term airtime lock (percent * 100).
    LtAlock(u16),
    /// Flow control: device ready for next packet.
    Ready,
    /// Selected subinterface changed.
    SelectedInterface(u8),
    /// Detailed firmware version string (e.g. "0.1.142-f63fb02").
    FirmwareDetail(String),
    /// Error code from device.
    Error(u8),
}

// ── Decoder ─────────────────────────────────────────────────────────────

/// Streaming RNode KISS decoder.
///
/// Handles KISS framing, KISS escape sequences, multi-byte command
/// responses, and subinterface data routing.
pub struct RNodeDecoder {
    in_frame: bool,
    escape: bool,
    command: u8,
    data_buffer: Vec<u8>,
    command_buffer: Vec<u8>,
    selected_index: u8,
}

impl RNodeDecoder {
    pub fn new() -> Self {
        RNodeDecoder {
            in_frame: false,
            escape: false,
            command: kiss::CMD_UNKNOWN,
            data_buffer: Vec::new(),
            command_buffer: Vec::new(),
            selected_index: 0,
        }
    }

    /// Current selected subinterface index.
    pub fn selected_index(&self) -> u8 {
        self.selected_index
    }

    /// Feed raw bytes and return decoded events.
    pub fn feed(&mut self, bytes: &[u8]) -> Vec<RNodeEvent> {
        let mut events = Vec::new();

        for &byte in bytes {
            if self.in_frame && byte == kiss::FEND {
                // End of frame — check if we have buffered data for a data command
                if let Some(idx) = data_cmd_to_index(self.command) {
                    if !self.data_buffer.is_empty() {
                        events.push(RNodeEvent::DataFrame {
                            index: idx,
                            data: core::mem::take(&mut self.data_buffer),
                        });
                    }
                } else if self.command == kiss::CMD_DATA {
                    if !self.data_buffer.is_empty() {
                        events.push(RNodeEvent::DataFrame {
                            index: self.selected_index as usize,
                            data: core::mem::take(&mut self.data_buffer),
                        });
                    }
                } else if self.command == CMD_FW_DETAIL {
                    if !self.data_buffer.is_empty() {
                        let s = String::from_utf8_lossy(&self.data_buffer).into_owned();
                        events.push(RNodeEvent::FirmwareDetail(s));
                        self.data_buffer.clear();
                    }
                }
                // Start new frame (closing FLAG = opening FLAG of next)
                self.in_frame = true;
                self.command = kiss::CMD_UNKNOWN;
                self.data_buffer.clear();
                self.command_buffer.clear();
                self.escape = false;
            } else if byte == kiss::FEND {
                // Opening frame
                self.in_frame = true;
                self.command = kiss::CMD_UNKNOWN;
                self.data_buffer.clear();
                self.command_buffer.clear();
                self.escape = false;
            } else if self.in_frame {
                if self.data_buffer.is_empty()
                    && self.command_buffer.is_empty()
                    && self.command == kiss::CMD_UNKNOWN
                {
                    // First byte after FEND is the command
                    self.command = byte;
                } else if self.command == kiss::CMD_DATA
                    || self.command == CMD_FW_DETAIL
                    || data_cmd_to_index(self.command).is_some()
                {
                    // Data frame: accumulate with KISS unescaping
                    if byte == kiss::FESC {
                        self.escape = true;
                    } else if self.escape {
                        match byte {
                            kiss::TFEND => self.data_buffer.push(kiss::FEND),
                            kiss::TFESC => self.data_buffer.push(kiss::FESC),
                            _ => self.data_buffer.push(byte),
                        }
                        self.escape = false;
                    } else {
                        self.data_buffer.push(byte);
                    }
                } else {
                    // Command response: accumulate with KISS unescaping, then parse
                    let val = if byte == kiss::FESC {
                        self.escape = true;
                        continue;
                    } else if self.escape {
                        self.escape = false;
                        match byte {
                            kiss::TFEND => kiss::FEND,
                            kiss::TFESC => kiss::FESC,
                            _ => byte,
                        }
                    } else {
                        byte
                    };

                    self.command_buffer.push(val);
                    self.parse_command(&mut events);
                }
            }
        }

        events
    }

    /// Check if a complete command response has been accumulated and emit event.
    fn parse_command(&mut self, events: &mut Vec<RNodeEvent>) {
        let buf = &self.command_buffer;
        match self.command {
            CMD_DETECT => {
                if buf.len() >= 1 {
                    events.push(RNodeEvent::Detected(buf[0] == DETECT_RESP));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_FW_VERSION => {
                if buf.len() >= 2 {
                    events.push(RNodeEvent::FirmwareVersion {
                        major: buf[0],
                        minor: buf[1],
                    });
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_PLATFORM => {
                if buf.len() >= 1 {
                    events.push(RNodeEvent::Platform(buf[0]));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_MCU => {
                if buf.len() >= 1 {
                    events.push(RNodeEvent::Mcu(buf[0]));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_INTERFACES => {
                if buf.len() >= 2 {
                    events.push(RNodeEvent::InterfaceType {
                        index: buf[0],
                        type_byte: buf[1],
                    });
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_FREQUENCY => {
                if buf.len() >= 4 {
                    let freq = (buf[0] as u32) << 24
                        | (buf[1] as u32) << 16
                        | (buf[2] as u32) << 8
                        | buf[3] as u32;
                    events.push(RNodeEvent::Frequency(freq));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_BANDWIDTH => {
                if buf.len() >= 4 {
                    let bw = (buf[0] as u32) << 24
                        | (buf[1] as u32) << 16
                        | (buf[2] as u32) << 8
                        | buf[3] as u32;
                    events.push(RNodeEvent::Bandwidth(bw));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_TXPOWER => {
                if buf.len() >= 1 {
                    events.push(RNodeEvent::TxPower(buf[0] as i8));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_SF => {
                if buf.len() >= 1 {
                    events.push(RNodeEvent::SpreadingFactor(buf[0]));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_CR => {
                if buf.len() >= 1 {
                    events.push(RNodeEvent::CodingRate(buf[0]));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_RADIO_STATE => {
                if buf.len() >= 1 {
                    events.push(RNodeEvent::RadioState(buf[0]));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_STAT_RSSI => {
                if buf.len() >= 1 {
                    events.push(RNodeEvent::StatRssi(buf[0]));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_STAT_SNR => {
                if buf.len() >= 1 {
                    events.push(RNodeEvent::StatSnr(buf[0] as i8));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_ST_ALOCK => {
                if buf.len() >= 2 {
                    let val = (buf[0] as u16) << 8 | buf[1] as u16;
                    events.push(RNodeEvent::StAlock(val));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_LT_ALOCK => {
                if buf.len() >= 2 {
                    let val = (buf[0] as u16) << 8 | buf[1] as u16;
                    events.push(RNodeEvent::LtAlock(val));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_READY => {
                events.push(RNodeEvent::Ready);
                self.command = kiss::CMD_UNKNOWN;
                self.in_frame = false;
            }
            CMD_SEL_INT => {
                if buf.len() >= 1 {
                    self.selected_index = buf[0];
                    events.push(RNodeEvent::SelectedInterface(buf[0]));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            CMD_ERROR => {
                if buf.len() >= 1 {
                    events.push(RNodeEvent::Error(buf[0]));
                    self.command = kiss::CMD_UNKNOWN;
                    self.in_frame = false;
                }
            }
            _ => {
                // Unknown command, ignore
            }
        }
    }
}

// ── Command builders ────────────────────────────────────────────────────

/// Build a KISS command frame: [FEND][cmd][escaped value][FEND].
pub fn rnode_command(cmd: u8, value: &[u8]) -> Vec<u8> {
    let escaped = kiss::escape(value);
    let mut out = Vec::with_capacity(escaped.len() + 3);
    out.push(kiss::FEND);
    out.push(cmd);
    out.extend_from_slice(&escaped);
    out.push(kiss::FEND);
    out
}

/// Build a command frame with subinterface selection prefix:
/// [FEND][CMD_SEL_INT][index][FEND][cmd][escaped value][FEND].
pub fn rnode_select_command(index: u8, cmd: u8, value: &[u8]) -> Vec<u8> {
    let mut out = rnode_command(CMD_SEL_INT, &[index]);
    out.extend_from_slice(&rnode_command(cmd, value));
    out
}

/// Build the detect request frame.
pub fn detect_request() -> Vec<u8> {
    rnode_command(CMD_DETECT, &[DETECT_REQ])
}

/// Build a data frame for a subinterface:
/// [FEND][CMD_INTn_DATA][escaped data][FEND].
pub fn rnode_data_frame(index: u8, data: &[u8]) -> Vec<u8> {
    let cmd = if (index as usize) < DATA_CMDS.len() {
        DATA_CMDS[index as usize]
    } else {
        CMD_INT0_DATA
    };
    let escaped = kiss::escape(data);
    let mut out = Vec::with_capacity(escaped.len() + 3);
    out.push(kiss::FEND);
    out.push(cmd);
    out.extend_from_slice(&escaped);
    out.push(kiss::FEND);
    out
}

// ── Tests ───────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn detect_request_format() {
        let req = detect_request();
        assert_eq!(req, vec![kiss::FEND, CMD_DETECT, DETECT_REQ, kiss::FEND]);
    }

    #[test]
    fn decoder_detect_response() {
        let response = vec![kiss::FEND, CMD_DETECT, DETECT_RESP, kiss::FEND];
        let mut decoder = RNodeDecoder::new();
        let events = decoder.feed(&response);
        assert_eq!(events.len(), 1);
        assert_eq!(events[0], RNodeEvent::Detected(true));
    }

    #[test]
    fn decoder_firmware_version() {
        // Version 1.52 with KISS escaping possible
        let response = vec![kiss::FEND, CMD_FW_VERSION, 0x01, 0x34, kiss::FEND];
        let mut decoder = RNodeDecoder::new();
        let events = decoder.feed(&response);
        assert_eq!(events.len(), 1);
        assert_eq!(
            events[0],
            RNodeEvent::FirmwareVersion {
                major: 1,
                minor: 0x34
            }
        );
    }

    #[test]
    fn decoder_platform() {
        let response = vec![kiss::FEND, CMD_PLATFORM, 0x80, kiss::FEND]; // ESP32
        let mut decoder = RNodeDecoder::new();
        let events = decoder.feed(&response);
        assert_eq!(events.len(), 1);
        assert_eq!(events[0], RNodeEvent::Platform(0x80));
    }

    #[test]
    fn decoder_interfaces() {
        let response = vec![kiss::FEND, CMD_INTERFACES, 0x00, 0x01, kiss::FEND];
        let mut decoder = RNodeDecoder::new();
        let events = decoder.feed(&response);
        assert_eq!(events.len(), 1);
        assert_eq!(
            events[0],
            RNodeEvent::InterfaceType {
                index: 0,
                type_byte: 0x01
            }
        );
    }

    #[test]
    fn decoder_frequency() {
        // 868200000 Hz = 0x33C15740 (but let's use a simpler value)
        // 867200000 Hz = 0x33B5_D100
        let freq: u32 = 867_200_000;
        let response = vec![
            kiss::FEND,
            CMD_FREQUENCY,
            (freq >> 24) as u8,
            (freq >> 16) as u8,
            (freq >> 8) as u8,
            (freq & 0xFF) as u8,
            kiss::FEND,
        ];
        let mut decoder = RNodeDecoder::new();
        let events = decoder.feed(&response);
        assert_eq!(events.len(), 1);
        assert_eq!(events[0], RNodeEvent::Frequency(867_200_000));
    }

    #[test]
    fn decoder_data_frame_int0() {
        let payload = vec![0x01, 0x02, 0x03, 0x04, 0x05];
        // CMD_INT0_DATA = 0x00 (same as CMD_DATA)
        let mut frame = vec![kiss::FEND, CMD_INT0_DATA];
        frame.extend_from_slice(&kiss::escape(&payload));
        frame.push(kiss::FEND);

        let mut decoder = RNodeDecoder::new();
        let events = decoder.feed(&frame);
        assert_eq!(events.len(), 1);
        assert_eq!(
            events[0],
            RNodeEvent::DataFrame {
                index: 0,
                data: payload
            }
        );
    }

    #[test]
    fn decoder_multi_sub_data() {
        let payload = vec![0xAA, 0xBB];
        // CMD_INT1_DATA = 0x10
        let mut frame = vec![kiss::FEND, CMD_INT1_DATA];
        frame.extend_from_slice(&kiss::escape(&payload));
        frame.push(kiss::FEND);

        let mut decoder = RNodeDecoder::new();
        let events = decoder.feed(&frame);
        assert_eq!(events.len(), 1);
        assert_eq!(
            events[0],
            RNodeEvent::DataFrame {
                index: 1,
                data: payload
            }
        );
    }

    #[test]
    fn rnode_select_command_format() {
        // Select subinterface 1, then set frequency
        let freq: u32 = 868_000_000;
        let freq_bytes = [
            (freq >> 24) as u8,
            (freq >> 16) as u8,
            (freq >> 8) as u8,
            (freq & 0xFF) as u8,
        ];
        let cmd = rnode_select_command(1, CMD_FREQUENCY, &freq_bytes);

        // Should start with [FEND][CMD_SEL_INT][0x01][FEND]
        assert_eq!(cmd[0], kiss::FEND);
        assert_eq!(cmd[1], CMD_SEL_INT);
        assert_eq!(cmd[2], 0x01);
        assert_eq!(cmd[3], kiss::FEND);

        // Then [FEND][CMD_FREQUENCY][escaped bytes][FEND]
        assert_eq!(cmd[4], kiss::FEND);
        assert_eq!(cmd[5], CMD_FREQUENCY);
    }

    #[test]
    fn rnode_data_frame_format() {
        let data = vec![0x01, 0x02, 0x03];
        let frame = rnode_data_frame(0, &data);
        assert_eq!(frame[0], kiss::FEND);
        assert_eq!(frame[1], CMD_INT0_DATA);
        assert_eq!(*frame.last().unwrap(), kiss::FEND);

        // Subinterface 1
        let frame1 = rnode_data_frame(1, &data);
        assert_eq!(frame1[1], CMD_INT1_DATA);

        // Subinterface 2
        let frame2 = rnode_data_frame(2, &data);
        assert_eq!(frame2[1], CMD_INT2_DATA);
    }
}