2020-09-17 17:02:01 +08:00
|
|
|
use log::info;
|
2020-09-21 17:31:34 +08:00
|
|
|
use nom::IResult;
|
|
|
|
use nom::combinator::{value, map, map_res, not, opt, all_consuming};
|
|
|
|
use nom::sequence::{terminated, preceded, pair, delimited, tuple};
|
|
|
|
use nom::bytes::complete::{take, tag, tag_no_case, take_while};
|
|
|
|
use nom::character::complete::digit1;
|
|
|
|
use nom::character::is_space;
|
|
|
|
use nom::branch::alt;
|
|
|
|
use nom::number::complete::{float, double};
|
|
|
|
|
|
|
|
use uom::si::f64::Frequency;
|
|
|
|
use uom::si::frequency::{hertz, kilohertz, megahertz, gigahertz};
|
2020-09-17 17:02:01 +08:00
|
|
|
|
|
|
|
use embedded_hal::blocking::spi::Transfer;
|
|
|
|
use core::convert::TryInto;
|
|
|
|
use crate::ClockSource as UrukulClockSource;
|
|
|
|
use crate::ClockSource::*;
|
|
|
|
use crate::Urukul;
|
|
|
|
use crate::Error;
|
|
|
|
|
2020-09-21 17:31:34 +08:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum MqttTopic {
|
|
|
|
Switch(u8),
|
|
|
|
Attenuation(u8),
|
|
|
|
Singletone(u8, u8),
|
|
|
|
SingletoneFrequency(u8, u8),
|
|
|
|
SingletoneAmplitude(u8, u8),
|
|
|
|
SingletonePhase(u8, u8),
|
|
|
|
Profile,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prossible change: Make this enum public to all comm protocol (if any)
|
|
|
|
// Such that Urukul accepts the enum directly
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum MqttCommand {
|
2020-09-17 17:02:01 +08:00
|
|
|
Switch(u8, bool),
|
|
|
|
Attenuation(u8, f32),
|
2020-09-21 17:31:34 +08:00
|
|
|
Singletone(u8, u8, f64, f64, f64),
|
|
|
|
SingletoneFrequency(u8, u8, f64),
|
|
|
|
SingletoneAmplitude(u8, u8, f64),
|
|
|
|
SingletonePhase(u8, u8, f64),
|
|
|
|
Profile(u8)
|
2020-09-17 17:02:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MqttMux<SPI> {
|
|
|
|
urukul: Urukul<SPI>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<SPI, E> MqttMux<SPI> where SPI: Transfer<u8, Error = E> {
|
|
|
|
pub fn new(urukul: Urukul<SPI>) -> Self {
|
|
|
|
MqttMux {
|
|
|
|
urukul
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 17:31:34 +08:00
|
|
|
pub fn process_mqtt(&mut self, topic: &str, message: &[u8]) -> Result<(), Error<E>> {
|
|
|
|
let (_, header) = self.parse_header(topic)
|
|
|
|
.map_err(|_| Error::MqttTopicError)?;
|
|
|
|
info!("{:?}", header);
|
|
|
|
let (_, command) = self.parse_message(header, message)
|
|
|
|
.map_err(|_| Error::MqttCommandError)?;
|
|
|
|
info!("{:?}", command);
|
2020-09-17 17:02:01 +08:00
|
|
|
self.execute(command)
|
|
|
|
}
|
|
|
|
|
2020-09-21 17:31:34 +08:00
|
|
|
fn parse_header<'a>(&mut self, topic: &'a str) -> IResult<&'a str, MqttTopic> {
|
|
|
|
preceded(
|
|
|
|
alt((
|
|
|
|
tag("Urukul/Control/"),
|
|
|
|
tag("/Urukul/Control/")
|
|
|
|
)),
|
|
|
|
alt((
|
|
|
|
switch,
|
|
|
|
attenuation,
|
|
|
|
singletone,
|
|
|
|
singletone_frequency,
|
|
|
|
singletone_amplitude,
|
|
|
|
singletone_phase,
|
|
|
|
profile // Note: Put profile at the end
|
|
|
|
))
|
|
|
|
)(topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_message<'a>(&mut self, topic: MqttTopic, message: &'a [u8]) -> IResult<&'a [u8], MqttCommand> {
|
|
|
|
match topic {
|
|
|
|
MqttTopic::Switch(ch) => switch_message(ch, message),
|
|
|
|
MqttTopic::Attenuation(ch) => attenuation_message(ch, message),
|
|
|
|
MqttTopic::Singletone(ch, prof) => singletone_message(ch, prof, message),
|
|
|
|
MqttTopic::SingletoneFrequency(ch, prof) => singletone_frequency_message(ch, prof, message),
|
|
|
|
MqttTopic::SingletoneAmplitude(ch, prof) => singletone_amplitude_message(ch, prof, message),
|
|
|
|
MqttTopic::SingletonePhase(ch, prof) => singletone_phase_message(ch, prof, message),
|
|
|
|
MqttTopic::Profile => profile_message(message),
|
2020-09-17 17:02:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 17:31:34 +08:00
|
|
|
fn execute(&mut self, command: MqttCommand) -> Result<(), Error<E>> {
|
|
|
|
match command {
|
|
|
|
MqttCommand::Switch(ch, state) => self.urukul.set_channel_switch(ch.into(), state),
|
|
|
|
MqttCommand::Attenuation(ch, ampl) => self.urukul.set_channel_attenuation(ch, ampl),
|
|
|
|
MqttCommand::Singletone(ch, prof, freq, ampl, deg) => self.urukul.set_channel_single_tone_profile(ch, prof, freq, ampl, deg),
|
|
|
|
MqttCommand::SingletoneFrequency(ch, prof, freq) => self.urukul.set_channel_single_tone_profile_frequency(ch, prof, freq),
|
|
|
|
MqttCommand::SingletoneAmplitude(ch, prof, ampl) => self.urukul.set_channel_single_tone_profile_amplitude(ch, prof, ampl),
|
|
|
|
MqttCommand::SingletonePhase(ch, prof, deg) => self.urukul.set_channel_single_tone_profile_phase(ch, prof, deg),
|
|
|
|
MqttCommand::Profile(prof) => self.urukul.set_profile(prof),
|
2020-09-17 17:02:01 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-21 17:31:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Topic separator parser
|
|
|
|
fn topic_separator<'a>(topic: &'a str) -> IResult<&'a str, ()> {
|
|
|
|
value((), tag("/"))(topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message separator parser
|
|
|
|
fn message_separator(message: &[u8]) -> IResult<&[u8], ()> {
|
|
|
|
value(
|
|
|
|
(),
|
|
|
|
preceded(
|
|
|
|
whitespace,
|
|
|
|
preceded(
|
|
|
|
tag("/"),
|
|
|
|
whitespace
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Selection parsers
|
|
|
|
fn select_channel<'a>(topic: &'a str) -> IResult<&'a str, u8> {
|
|
|
|
terminated(
|
|
|
|
map_res(
|
|
|
|
preceded(
|
|
|
|
tag("Channel"),
|
|
|
|
digit1
|
|
|
|
),
|
|
|
|
|num: &str| u8::from_str_radix(num, 10)
|
|
|
|
),
|
|
|
|
topic_separator
|
|
|
|
)(topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn select_profile<'a>(topic: &'a str) -> IResult<&'a str, u8> {
|
|
|
|
terminated(
|
|
|
|
map_res(
|
|
|
|
preceded(
|
|
|
|
tag("Profile"),
|
|
|
|
digit1
|
|
|
|
),
|
|
|
|
|num: &str| u8::from_str_radix(num, 10)
|
|
|
|
),
|
|
|
|
topic_separator
|
|
|
|
)(topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Selection parser for Singletone
|
|
|
|
// Note: This fucntion assumes singletone is not the most specific sub-topic
|
|
|
|
fn select_singletone<'a>(topic: &'a str) -> IResult<&'a str, ()> {
|
|
|
|
preceded(
|
|
|
|
tag("Singletone"),
|
|
|
|
topic_separator
|
|
|
|
)(topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_end_slice(message: &[u8]) -> IResult<&[u8], ()> {
|
|
|
|
not(
|
|
|
|
take(1_usize)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read whitespace
|
|
|
|
fn whitespace(message: &[u8]) -> IResult<&[u8], ()> {
|
|
|
|
value((), take_while(is_space))(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reader for uom instances
|
|
|
|
fn read_frequency(message: &[u8]) -> IResult<&[u8], f64> {
|
|
|
|
map(
|
|
|
|
pair(
|
|
|
|
double,
|
|
|
|
opt(
|
|
|
|
preceded(
|
|
|
|
whitespace,
|
|
|
|
alt((
|
|
|
|
value(1.0, tag_no_case("hz")),
|
|
|
|
value(1_000.0, tag_no_case("khz")),
|
|
|
|
value(1_000_000.0, tag_no_case("mhz")),
|
|
|
|
value(1_000_000_000.0, tag_no_case("ghz"))
|
|
|
|
))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
|(freq, unit): (f64, Option<f64>)| {
|
|
|
|
freq * unit.map_or(1.0, |mul| mul)
|
|
|
|
}
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Switch Command Topic
|
|
|
|
fn switch<'a>(topic: &'a str) -> IResult<&'a str, MqttTopic> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
terminated(
|
|
|
|
select_channel,
|
|
|
|
tag("Switch")
|
|
|
|
),
|
|
|
|
|channel: u8| MqttTopic::Switch(channel)
|
|
|
|
)
|
|
|
|
)(topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Switch Command Message
|
|
|
|
fn switch_message(channel: u8, message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
alt((
|
|
|
|
value(true, tag("on")),
|
|
|
|
value(false, tag("off"))
|
|
|
|
)),
|
|
|
|
|switch| MqttCommand::Switch(channel, switch)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Attenuation Command Topic
|
|
|
|
fn attenuation<'a>(topic: &'a str) -> IResult<&'a str, MqttTopic> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
terminated(
|
|
|
|
select_channel,
|
|
|
|
tag("Attenuation")
|
|
|
|
),
|
|
|
|
|channel: u8| MqttTopic::Attenuation(channel)
|
|
|
|
)
|
|
|
|
)(topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Attenuation Command Message
|
|
|
|
fn attenuation_message(channel: u8, message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
terminated(
|
|
|
|
float,
|
|
|
|
opt(
|
|
|
|
preceded(
|
|
|
|
whitespace,
|
|
|
|
tag_no_case("db")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
|att: f32| MqttCommand::Attenuation(channel, att)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Singletone frequenct Command Topic
|
|
|
|
fn singletone_frequency<'a>(topic: &'a str) -> IResult<&'a str, MqttTopic> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
pair(
|
|
|
|
select_channel,
|
|
|
|
terminated(
|
|
|
|
select_profile,
|
|
|
|
preceded(
|
|
|
|
select_singletone,
|
|
|
|
tag("Frequency"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
|(channel, profile): (u8, u8)| MqttTopic::SingletoneFrequency(channel, profile)
|
|
|
|
)
|
|
|
|
)(topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Singletone frequency Command Message
|
|
|
|
fn singletone_frequency_message(channel: u8, profile: u8, message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
read_frequency,
|
|
|
|
|freq: f64| MqttCommand::SingletoneFrequency(channel, profile, freq)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Singletone Amplitude Command Topic
|
|
|
|
fn singletone_amplitude<'a>(topic: &'a str) -> IResult<&'a str, MqttTopic> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
pair(
|
|
|
|
select_channel,
|
|
|
|
terminated(
|
|
|
|
select_profile,
|
|
|
|
preceded(
|
|
|
|
select_singletone,
|
|
|
|
tag("Amplitude"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
|(channel, profile): (u8, u8)| MqttTopic::SingletoneAmplitude(channel, profile)
|
|
|
|
)
|
|
|
|
)(topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Singletone AMplitude Command Message
|
|
|
|
fn singletone_amplitude_message(channel: u8, profile: u8, message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
double,
|
|
|
|
|ampl: f64| MqttCommand::SingletoneAmplitude(channel, profile, ampl)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Phase Command Topic
|
|
|
|
fn singletone_phase<'a>(topic: &'a str) -> IResult<&'a str, MqttTopic> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
pair(
|
|
|
|
select_channel,
|
|
|
|
terminated(
|
|
|
|
select_profile,
|
|
|
|
preceded(
|
|
|
|
select_singletone,
|
|
|
|
tag("Phase"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
|(channel, profile): (u8, u8)| MqttTopic::SingletonePhase(channel, profile)
|
|
|
|
)
|
|
|
|
)(topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for Phase Command Message
|
|
|
|
fn singletone_phase_message(channel: u8, profile: u8, message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
terminated(
|
|
|
|
double,
|
|
|
|
opt(
|
|
|
|
preceded(
|
|
|
|
whitespace,
|
|
|
|
tag_no_case("deg")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
|deg: f64| MqttCommand::SingletonePhase(channel, profile, deg)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for one-command singletone profile Topic
|
|
|
|
fn singletone<'a>(topic: &'a str) -> IResult<&'a str, MqttTopic> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
pair(
|
|
|
|
select_channel,
|
|
|
|
terminated(
|
|
|
|
select_profile,
|
|
|
|
tag("Singletone")
|
|
|
|
)
|
|
|
|
),
|
|
|
|
|(channel, profile): (u8, u8)| MqttTopic::Singletone(channel, profile)
|
|
|
|
)
|
|
|
|
)(topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parser for one-command singletone profile Command
|
|
|
|
fn singletone_message(channel: u8, profile: u8, message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
tuple((
|
|
|
|
read_frequency,
|
|
|
|
preceded(
|
|
|
|
message_separator,
|
|
|
|
double
|
|
|
|
),
|
|
|
|
preceded(
|
|
|
|
message_separator,
|
|
|
|
terminated(
|
|
|
|
double,
|
|
|
|
opt(
|
|
|
|
preceded(
|
|
|
|
whitespace,
|
|
|
|
tag_no_case("deg")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
)),
|
|
|
|
|(freq, ampl, phase): (f64, f64, f64)| MqttCommand::Singletone(channel, profile, freq, ampl, phase)
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn profile<'a>(topic: &'a str) -> IResult<&'a str, MqttTopic> {
|
|
|
|
all_consuming(
|
|
|
|
value(
|
|
|
|
MqttTopic::Profile,
|
|
|
|
tag("Profile")
|
|
|
|
)
|
|
|
|
)(topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn profile_message(message: &[u8]) -> IResult<&[u8], MqttCommand> {
|
|
|
|
all_consuming(
|
|
|
|
map(
|
|
|
|
digit1,
|
|
|
|
|num: &[u8]| {
|
|
|
|
MqttCommand::Profile(
|
|
|
|
u8::from_str_radix(core::str::from_utf8(num).unwrap(), 10).unwrap()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)(message)
|
|
|
|
}
|