radio 0.12.1

Generic traits for embedded packet radio devices
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
//! Provides common helpers for implementing radio utilities
//!
//! ## <https://github.com/rust-iot/radio-hal>
//! ## Copyright 2020-2022 Ryan Kurte

use std::ffi::CString;
use std::fs::{File, OpenOptions};
use std::prelude::v1::*;
use std::string::String;
use std::time::SystemTime;

use libc::{self};

#[cfg(not(feature = "defmt"))]
use log::{debug, info};

#[cfg(feature = "defmt")]
use defmt::{debug, info};

use clap::Parser;
use embedded_hal::delay::DelayNs;
use humantime::Duration as HumanDuration;

use byteorder::{ByteOrder, NetworkEndian};
use pcap_file::{pcap::PcapHeader, DataLink, PcapWriter};
use rolling_stats::Stats;

use crate::*;
use crate::{
    blocking::{BlockingError, BlockingOptions, BlockingReceive, BlockingTransmit},
    Power, Receive, ReceiveInfo, Rssi, Transmit,
};

/// Basic operations supported by the helpers package
#[derive(Clone, Parser, PartialEq, Debug)]
pub enum Operation {
    #[clap(name = "tx")]
    /// Transmit a packet
    Transmit(TransmitOptions),

    #[clap(name = "rx")]
    /// Receive a packet
    Receive(ReceiveOptions),

    #[clap(name = "rssi")]
    /// Poll RSSI on the configured channel
    Rssi(RssiOptions),

    #[clap(name = "echo")]
    /// Echo back received messages (useful with Link Test mode)
    Echo(EchoOptions),

    #[clap(name = "ping-pong")]
    /// Link test (ping-pong) mode
    LinkTest(PingPongOptions),
}

pub fn do_operation<T, I, E>(radio: &mut T, operation: Operation) -> Result<(), BlockingError<E>>
where
    T: Transmit<Error = E>
        + Power<Error = E>
        + Receive<Info = I, Error = E>
        + Rssi<Error = E>
        + Power<Error = E>
        + DelayNs,
    I: ReceiveInfo + Default + std::fmt::Debug,
    E: std::fmt::Debug,
{
    let mut buff = [0u8; 1024];

    // TODO: the rest
    match operation {
        Operation::Transmit(options) => do_transmit(radio, options)?,
        Operation::Receive(options) => do_receive(radio, &mut buff, options).map(|_| ())?,
        Operation::Echo(options) => do_echo(radio, &mut buff, options).map(|_| ())?,
        Operation::Rssi(options) => do_rssi(radio, options).map(|_| ())?,
        Operation::LinkTest(options) => do_ping_pong(radio, options).map(|_| ())?,
        //_ => warn!("unsuppored command: {:?}", opts.command),
    }

    Ok(())
}

/// Configuration for Transmit operation
#[derive(Clone, Parser, PartialEq, Debug)]
pub struct TransmitOptions {
    /// Data to be transmitted
    #[clap(long)]
    pub data: Vec<u8>,

    /// Power in dBm (range -18dBm to 13dBm)
    #[clap(long)]
    pub power: Option<i8>,

    /// Specify period for repeated transmission
    #[clap(long)]
    pub period: Option<HumanDuration>,

    #[clap(flatten)]
    pub blocking_options: BlockingOptions,
}

pub fn do_transmit<T, E>(radio: &mut T, options: TransmitOptions) -> Result<(), BlockingError<E>>
where
    T: Transmit<Error = E> + Power<Error = E> + DelayNs,
    E: core::fmt::Debug,
{
    // Set output power if specified
    if let Some(p) = options.power {
        radio.set_power(p)?;
    }

    loop {
        // Transmit packet
        radio.do_transmit(&options.data, options.blocking_options.clone())?;

        // Delay for repeated transmission or exit
        match &options.period {
            Some(p) => radio.delay_us(p.as_micros() as u32),
            None => break,
        }
    }

    Ok(())
}

/// Configuration for Receive operation
#[derive(Clone, Parser, PartialEq, Debug)]
pub struct ReceiveOptions {
    /// Run continuously
    #[clap(long = "continuous")]
    pub continuous: bool,

    #[clap(flatten)]
    pub pcap_options: PcapOptions,

    #[clap(flatten)]
    pub blocking_options: BlockingOptions,
}

#[derive(Clone, Parser, PartialEq, Debug)]

pub struct PcapOptions {
    /// Create and write capture output to a PCAP file
    #[clap(long, group = "1")]
    pub pcap_file: Option<String>,

    /// Create and write to a unix pipe for connection to wireshark
    #[clap(long, group = "1")]
    pub pcap_pipe: Option<String>,
}

impl PcapOptions {
    pub fn open(&self) -> Result<Option<PcapWriter<File>>, std::io::Error> {
        // Open file or pipe if specified
        let pcap_file = match (&self.pcap_file, &self.pcap_pipe) {
            // Open as file
            (Some(file), None) => {
                let f = File::create(file)?;
                Some(f)
            }
            // Open as pipe
            #[cfg(target_family = "unix")]
            (None, Some(pipe)) => {
                // Ensure file doesn't already exist
                let _ = std::fs::remove_file(pipe);

                // Create pipe
                let n = CString::new(pipe.as_str()).unwrap();
                let status = unsafe { libc::mkfifo(n.as_ptr(), 0o644) };

                // Manual status code handling
                // TODO: return io::Error
                if status != 0 {
                    panic!("Error creating fifo: {}", status);
                }

                // Open pipe
                let f = OpenOptions::new()
                    .write(true)
                    .open(pipe)
                    .expect("Error opening PCAP pipe");

                Some(f)
            }

            (None, None) => None,

            _ => unimplemented!(),
        };

        #[cfg(any(feature = "log", feature = "defmt"))]
        info!("pcap pipe open, awaiting connection");

        // Setup pcap writer and write header
        // (This is a blocking operation on pipes)
        let pcap_writer = match pcap_file {
            None => None,
            Some(f) => {
                // Setup pcap header
                let mut h = PcapHeader::default();
                h.datalink = DataLink::IEEE802_15_4;

                // Write header
                let w = PcapWriter::with_header(h, f).expect("Error writing to PCAP file");
                Some(w)
            }
        };

        Ok(pcap_writer)
    }
}

/// Receive from the radio using the provided configuration
pub fn do_receive<T, I, E>(
    radio: &mut T,
    mut buff: &mut [u8],
    options: ReceiveOptions,
) -> Result<usize, E>
where
    T: Receive<Info = I, Error = E> + DelayNs,
    I: std::fmt::Debug,
    E: std::fmt::Debug,
{
    // Create and open pcap file for writing
    let mut pcap_writer = options
        .pcap_options
        .open()
        .expect("Error opening pcap file / pipe");

    // Start receive mode
    radio.start_receive()?;

    loop {
        if radio.check_receive(true)? {
            let (n, i) = radio.get_received(&mut buff)?;

            match std::str::from_utf8(&buff[0..n as usize]) {
                Ok(s) => info!("Received: '{}' info: {:?}", s, i),
                #[cfg(not(feature = "defmt"))]
                Err(_) => info!("Received: '{:02x?}' info: {:?}", &buff[0..n as usize], i),
                #[cfg(feature = "defmt")]
                Err(_) => info!("Received: '{:?}' info: {:?}", &buff[0..n as usize], i),
            }

            if let Some(p) = &mut pcap_writer {
                let t = SystemTime::now()
                    .duration_since(SystemTime::UNIX_EPOCH)
                    .unwrap();

                p.write(
                    t.as_secs() as u32,
                    t.as_nanos() as u32 % 1_000_000,
                    &buff[0..n],
                    n as u32,
                )
                .expect("Error writing pcap file");
            }

            if !options.continuous {
                return Ok(n);
            }

            radio.start_receive()?;
        }

        radio.delay_us(options.blocking_options.poll_interval.as_micros() as u32);
    }
}

/// Configuration for RSSI operation
#[derive(Clone, Parser, PartialEq, Debug)]
pub struct RssiOptions {
    /// Specify period for RSSI polling
    #[clap(long = "period", default_value = "1s")]
    pub period: HumanDuration,

    /// Run continuously
    #[clap(long = "continuous")]
    pub continuous: bool,
}

pub fn do_rssi<T, I, E>(radio: &mut T, options: RssiOptions) -> Result<(), E>
where
    T: Receive<Info = I, Error = E> + Rssi<Error = E> + DelayNs,
    I: std::fmt::Debug,
    E: std::fmt::Debug,
{
    // Enter receive mode
    radio.start_receive()?;

    // Poll for RSSI
    loop {
        let rssi = radio.poll_rssi()?;

        info!("rssi: {}", rssi);

        radio.check_receive(true)?;

        radio.delay_us(options.period.as_micros() as u32);

        if !options.continuous {
            break;
        }
    }

    Ok(())
}

/// Configuration for Echo operation
#[derive(Clone, Parser, PartialEq, Debug)]
pub struct EchoOptions {
    /// Run continuously
    #[clap(long = "continuous")]
    pub continuous: bool,

    /// Power in dBm (range -18dBm to 13dBm)
    #[clap(long = "power")]
    pub power: Option<i8>,

    /// Specify delay for response message
    #[clap(long = "delay", default_value = "100ms")]
    pub delay: HumanDuration,

    /// Append RSSI and LQI to repeated message
    #[clap(long = "append-info")]
    pub append_info: bool,

    #[clap(flatten)]
    pub blocking_options: BlockingOptions,
}

pub fn do_echo<T, I, E>(
    radio: &mut T,
    mut buff: &mut [u8],
    options: EchoOptions,
) -> Result<usize, BlockingError<E>>
where
    T: Receive<Info = I, Error = E> + Transmit<Error = E> + Power<Error = E> + DelayNs,
    I: ReceiveInfo + std::fmt::Debug,
    E: std::fmt::Debug,
{
    // Set output power if specified
    if let Some(p) = options.power {
        radio.set_power(p)?;
    }

    // Start receive mode
    radio.start_receive()?;

    loop {
        if radio.check_receive(true)? {
            // Fetch received packet
            let (mut n, i) = radio.get_received(&mut buff)?;

            // Parse out string if possible, otherwise print hex
            match std::str::from_utf8(&buff[0..n as usize]) {
                Ok(s) => info!("Received: '{}' info: {:?}", s, i),
                #[cfg(not(feature = "defmt"))]
                Err(_) => info!("Received: '{:02x?}' info: {:?}", &buff[0..n as usize], i),
                #[cfg(feature = "defmt")]
                Err(_) => info!("Received: '{:?}' info: {:?}", &buff[0..n as usize], i),
            }

            // Append info if provided
            if options.append_info {
                NetworkEndian::write_i16(&mut buff[n..], i.rssi());
                n += 2;
            }

            // Wait for turnaround delay
            radio.delay_us(options.delay.as_micros() as u32);

            // Transmit respobnse
            radio.do_transmit(&buff[..n], options.blocking_options.clone())?;

            // Exit if non-continuous
            if !options.continuous {
                return Ok(n);
            }
        }

        // Wait for poll delay
        radio.delay_us(options.blocking_options.poll_interval.as_micros() as u32);
    }
}

/// Configuration for Echo operation
#[derive(Clone, Parser, PartialEq, Debug)]
pub struct PingPongOptions {
    /// Specify the number of rounds to tx/rx
    #[clap(long, default_value = "100")]
    pub rounds: u32,

    /// Power in dBm (range -18dBm to 13dBm)
    #[clap(long)]
    pub power: Option<i8>,

    /// Specify delay for response message
    #[clap(long, default_value = "100ms")]
    pub delay: HumanDuration,

    /// Parse RSSI and other info from response messages
    /// (echo server must have --append-info set)
    #[clap(long)]
    pub parse_info: bool,

    #[clap(flatten)]
    pub blocking_options: BlockingOptions,
}

pub struct LinkTestInfo {
    pub sent: u32,
    pub received: u32,
    pub local_rssi: Stats<f32>,
    pub remote_rssi: Stats<f32>,
}

pub fn do_ping_pong<T, I, E>(
    radio: &mut T,
    options: PingPongOptions,
) -> Result<LinkTestInfo, BlockingError<E>>
where
    T: Receive<Info = I, Error = E> + Transmit<Error = E> + Power<Error = E> + DelayNs,
    I: ReceiveInfo,
    E: std::fmt::Debug,
{
    let mut link_info = LinkTestInfo {
        sent: options.rounds,
        received: 0,
        local_rssi: Stats::new(),
        remote_rssi: Stats::new(),
    };

    let mut buff = [0u8; 32];

    // Set output power if specified
    if let Some(p) = options.power {
        radio.set_power(p)?;
    }

    for i in 0..options.rounds {
        // Encode message
        NetworkEndian::write_u32(&mut buff[0..], i as u32);
        let n = 4;

        debug!("Sending message {}", i);

        // Send message
        radio.do_transmit(&buff[0..n], options.blocking_options.clone())?;

        // Await response
        let (n, info) = match radio.do_receive(&mut buff, options.blocking_options.clone()) {
            Ok(r) => r,
            Err(BlockingError::Timeout) => {
                debug!("Timeout awaiting response {}", i);
                continue;
            }
            Err(e) => return Err(e),
        };

        let receive_index = NetworkEndian::read_u32(&buff[0..n]);
        if receive_index != i {
            debug!("Invalid receive index");
            continue;
        }

        // Parse info if provided
        let remote_rssi = match options.parse_info {
            true => Some(NetworkEndian::read_i16(&buff[4..n])),
            false => None,
        };

        debug!(
            "Received response {} with local rssi: {} and remote rssi: {:?}",
            receive_index,
            info.rssi(),
            remote_rssi
        );

        link_info.received += 1;
        link_info.local_rssi.update(info.rssi() as f32);
        if let Some(rssi) = remote_rssi {
            link_info.remote_rssi.update(rssi as f32);
        }

        // Wait for send delay
        radio.delay_us(options.delay.as_micros() as u32);
    }

    Ok(link_info)
}