rfe 0.1.0

Communicate with RF Explorer spectrum analyzers and signal generators over USB serial
Documentation
use std::time::Duration;

use chrono::{DateTime, Utc};
use nom::{Parser, bytes::complete::tag};

use crate::{
    common::{Frequency, MessageParseError},
    rf_explorer::parsers::*,
    signal_generator::{Attenuation, PowerLevel, RfPower, parsers::*},
};

/// Main-module frequency sweep configuration.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
pub struct ConfigFreqSweep {
    /// Start frequency.
    pub start: Frequency,
    /// Total number of sweep steps.
    pub total_steps: u32,
    /// Frequency increment per step.
    pub step: Frequency,
    /// RF output attenuation setting.
    pub attenuation: Attenuation,
    /// RF output power level.
    pub power_level: PowerLevel,
    /// RF output power state.
    pub rf_power: RfPower,
    /// Delay between sweep steps.
    pub sweep_delay: Duration,
    /// Time when this configuration was received.
    pub timestamp: DateTime<Utc>,
}
impl ConfigFreqSweep {
    pub(crate) const PREFIX: &'static [u8] = b"#C3-F:";
}

impl<'a> TryFrom<&'a [u8]> for ConfigFreqSweep {
    type Error = MessageParseError<'a>;

    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        // Parse the prefix of the message
        let (bytes, _) = tag(Self::PREFIX)(bytes)?;

        // Parse the start frequency
        let (bytes, start_khz) = freq_parser(7u8).parse(bytes)?;

        let (bytes, _) = parse_comma(bytes)?;

        // Parse the total steps
        let (bytes, total_steps) = num_parser(4u8).parse(bytes)?;

        let (bytes, _) = parse_comma(bytes)?;

        // Parse the step frequency
        let (bytes, step_khz) = freq_parser(7u8).parse(bytes)?;

        let (bytes, _) = parse_comma(bytes)?;

        // Parse the attenuation
        let (bytes, attenuation) = parse_attenuation(bytes)?;

        let (bytes, _) = parse_comma(bytes)?;

        // Parse the power level
        let (bytes, power_level) = parse_power_level(bytes)?;

        let (bytes, _) = parse_comma(bytes)?;

        // Parse the rf power
        let (bytes, rf_power) = parse_rf_power(bytes)?;

        let (bytes, _) = parse_comma(bytes)?;

        // Parse the sweep delay
        let (bytes, sweep_delay_ms) = parse_sweep_delay_ms(bytes)?;

        // Consume any \r or \r\n line endings and make sure there aren't any bytes left
        let _ = parse_opt_line_ending(bytes)?;

        Ok(ConfigFreqSweep {
            start: Frequency::from_khz(start_khz),
            total_steps,
            step: Frequency::from_khz(step_khz),
            attenuation,
            power_level,
            rf_power,
            sweep_delay: Duration::from_millis(u64::from(sweep_delay_ms)),
            timestamp: Utc::now(),
        })
    }
}

/// Expansion-module frequency sweep configuration.
#[derive(Debug, Copy, Clone, PartialEq, Default)]
pub struct ConfigFreqSweepExp {
    /// Start frequency.
    pub start: Frequency,
    /// Total number of sweep steps.
    pub total_steps: u32,
    /// Frequency increment per step.
    pub step: Frequency,
    /// Output power in dBm.
    pub power_dbm: f32,
    /// RF output power state.
    pub rf_power: RfPower,
    /// Delay between sweep steps.
    pub sweep_delay: Duration,
    /// Time when this configuration was received.
    pub timestamp: DateTime<Utc>,
}

impl ConfigFreqSweepExp {
    /// Message prefix used by expansion-module frequency sweep configuration messages.
    pub const PREFIX: &'static [u8] = b"#C5-F:";
}

impl<'a> TryFrom<&'a [u8]> for ConfigFreqSweepExp {
    type Error = MessageParseError<'a>;

    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        // Parse the prefix of the message
        let (bytes, _) = tag(Self::PREFIX)(bytes)?;

        // Parse the start frequency
        let (bytes, start_khz) = freq_parser(7u8).parse(bytes)?;

        let (bytes, _) = parse_comma(bytes)?;

        // Parse the total steps
        let (bytes, total_steps) = num_parser(4u8).parse(bytes)?;

        let (bytes, _) = parse_comma(bytes)?;

        // Parse the step frequency
        let (bytes, step_khz) = freq_parser(7u8).parse(bytes)?;

        let (bytes, _) = parse_comma(bytes)?;

        // Parse the power
        let (bytes, power_dbm) = num_parser(5u8).parse(bytes)?;

        let (bytes, _) = parse_comma(bytes)?;

        // Parse the rf power
        let (bytes, rf_power) = parse_rf_power(bytes)?;

        let (bytes, _) = parse_comma(bytes)?;

        // Parse the sweep delay
        let (bytes, sweep_delay_ms) = parse_sweep_delay_ms(bytes)?;

        // Consume any \r or \r\n line endings and make sure there aren't any bytes left
        let _ = parse_opt_line_ending(bytes)?;

        Ok(ConfigFreqSweepExp {
            start: Frequency::from_khz(start_khz),
            total_steps,
            step: Frequency::from_khz(step_khz),
            power_dbm,
            rf_power,
            sweep_delay: Duration::from_millis(u64::from(sweep_delay_ms)),
            timestamp: Utc::now(),
        })
    }
}

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

    #[test]
    fn parse_config_freq_sweep() {
        let bytes = b"#C3-F:0186525,0005,0001000,0,3,0,00100";
        let config_freq_sweep = ConfigFreqSweep::try_from(bytes.as_ref()).unwrap();
        assert_eq!(config_freq_sweep.start.as_khz(), 186_525);
        assert_eq!(config_freq_sweep.total_steps, 5);
        assert_eq!(config_freq_sweep.step.as_khz(), 1_000);
        assert_eq!(config_freq_sweep.attenuation, Attenuation::On);
        assert_eq!(config_freq_sweep.power_level, PowerLevel::Highest);
        assert_eq!(config_freq_sweep.rf_power, RfPower::On);
        assert_eq!(config_freq_sweep.sweep_delay.as_millis(), 100);
    }
}