From 770a4681774e262994528612d82b00366eac0a8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 21 Apr 2021 14:45:12 +0200 Subject: [PATCH 01/12] prepare v0.5.0 release * update changelog * bump version --- CHANGELOG.md | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 2 +- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a42efa..d6f355e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,73 @@ -# Changelog +# Change Log -## [v0.2.0] 2019-05-28 +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/) +and this project adheres to [Semantic Versioning](http://semver.org/). + +## [Unreleased] + +### Added + +### Changed + +### Fixed + +## [v0.5.0] - 2021-04-21 + +### Added + +* Batch sample processing +* DMA for ADC and DAC batches +* Pounder profile streaming +* DSP library with lots of optimized algorithms +* Digital input support +* Hardware in the loop continuous integration testing +* Dependency updates +* MQTT settings interface through miniconf/minimq +* Multi-binary support +* DHCP support + +### Changed + +* Removed JSON-over-TCP interface + +### Fixed + +* Rebust EEPROM MAC address reading slow supply start + +## [v0.4.1] - 2020-06-23 + +### Fixed + +* Fix DAC clr/ldac, SPI speed + +## [v0.4.0] - 2020-06-22 + +### Added + +* Hardware v1.1 only +* AD9959/Pounder support + +### Changed + +* HAL port + +## [v0.3.0] - 2020-01-20 + +### Added + +* Red LED handling +* EEPROM MAC address reading + +### Changed + +* Panic handler cleanup +* Dependency updates (smoltcp, rtfm) + +## [v0.2.0] - 2019-05-28 + +### Added * Initial basic release * Ethernet support @@ -9,6 +76,15 @@ * ADC/DAC timing and interrupts * Board configuration, bootstrap -## [v0.1.0] 2019-03-10 +## [v0.1.0] - 2019-03-10 + +### Added * First bits of code published + +[Unreleased]: https://github.com/quartiq/stabilizer/compare/v0.5.0...HEAD +[v0.5.0]: https://github.com/quartiq/stabilizer/compare/v0.4.1...v0.5.0 +[v0.4.1]: https://github.com/quartiq/stabilizer/compare/v0.4.0...v0.4.1 +[v0.4.0]: https://github.com/quartiq/stabilizer/compare/v0.3.0...v0.4.0 +[v0.3.0]: https://github.com/quartiq/stabilizer/compare/v0.2.0...v0.3.0 +[v0.2.0]: https://github.com/quartiq/stabilizer/compare/v0.1.0...v0.2.0 diff --git a/Cargo.toml b/Cargo.toml index f4db4d8..fe2f6ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "stabilizer" -version = "0.4.1" +version = "0.5.0" authors = ["Robert Jördens "] description = "Firmware for the Sinara Stabilizer board (stm32h743, eth, poe, 2 adc, 2 dac)" categories = ["embedded", "no-std", "hardware-support", "science"] From e5557582a2558a417e58e9e04bc2b1423bd3afe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 21 Apr 2021 15:03:28 +0200 Subject: [PATCH 02/12] let bors handle hitl --- .github/bors.toml | 1 + .github/workflows/hitl_trigger.yml | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/bors.toml b/.github/bors.toml index 722246d..5d1bf89 100644 --- a/.github/bors.toml +++ b/.github/bors.toml @@ -4,4 +4,5 @@ status = [ "style", "test (stable)", "compile (stable)", + "HITL Run Status" ] diff --git a/.github/workflows/hitl_trigger.yml b/.github/workflows/hitl_trigger.yml index 62abb96..b275421 100644 --- a/.github/workflows/hitl_trigger.yml +++ b/.github/workflows/hitl_trigger.yml @@ -2,8 +2,10 @@ name: HITL Trigger on: workflow_dispatch: - pull_request: - branches: [ master ] + push: + branches: + - staging + - trying jobs: hitl-trigger: From d9129ed02858437198d352b20dd4beb483f2e185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 21 Apr 2021 16:38:04 +0200 Subject: [PATCH 03/12] update lock --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 970506d..5e48b40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -726,7 +726,7 @@ dependencies = [ [[package]] name = "stabilizer" -version = "0.4.1" +version = "0.5.0" dependencies = [ "ad9959", "asm-delay", From 3a19d3dc6db8f40884de0b610baa927a1a497b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 21 Apr 2021 16:40:30 +0200 Subject: [PATCH 04/12] changelog: spelling --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6f355e..04aa2fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,7 +34,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed -* Rebust EEPROM MAC address reading slow supply start +* Robust EEPROM MAC address reading slow supply start ## [v0.4.1] - 2020-06-23 From 36cc423f70081886ae306c5b9fd9486b9d96bbeb Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Thu, 22 Apr 2021 15:16:33 +0200 Subject: [PATCH 05/12] Refactoring MQTT to support telemetry --- Cargo.lock | 6 +- Cargo.toml | 4 +- src/bin/dual-iir.rs | 22 +++--- src/bin/lockin-external.rs | 22 +++--- src/net/mod.rs | 148 ++++++++++++++++++++++++++++++------- 5 files changed, 146 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 970506d..ff026ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -203,7 +203,7 @@ dependencies = [ [[package]] name = "derive_miniconf" version = "0.1.0" -source = "git+https://github.com/quartiq/miniconf.git?rev=314fa5587d#314fa5587d1aa28e1ad70106f19e30db646e9f28" +source = "git+https://github.com/quartiq/miniconf.git?branch=feature/mqtt-removal#6b6b9d7973ac5b4771c211c775c51f82d4a7727f" dependencies = [ "proc-macro2", "quote", @@ -416,11 +416,10 @@ dependencies = [ [[package]] name = "miniconf" version = "0.1.0" -source = "git+https://github.com/quartiq/miniconf.git?rev=314fa5587d#314fa5587d1aa28e1ad70106f19e30db646e9f28" +source = "git+https://github.com/quartiq/miniconf.git?branch=feature/mqtt-removal#6b6b9d7973ac5b4771c211c775c51f82d4a7727f" dependencies = [ "derive_miniconf", "heapless 0.6.1", - "minimq", "serde", "serde-json-core", ] @@ -742,6 +741,7 @@ dependencies = [ "log", "mcp23017", "miniconf", + "minimq", "nb 1.0.0", "panic-semihosting", "paste", diff --git a/Cargo.toml b/Cargo.toml index f4db4d8..db89ef5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,13 +56,13 @@ version = "0.9.0" [patch.crates-io.miniconf] git = "https://github.com/quartiq/miniconf.git" -rev = "314fa5587d" +branch = "feature/mqtt-removal" [dependencies.smoltcp-nal] git = "https://github.com/quartiq/smoltcp-nal.git" rev = "8468f11" -[patch.crates-io.minimq] +[dependencies.minimq] git = "https://github.com/quartiq/minimq.git" rev = "933687c2e4b" diff --git a/src/bin/dual-iir.rs b/src/bin/dual-iir.rs index eaa82ae..b275de0 100644 --- a/src/bin/dual-iir.rs +++ b/src/bin/dual-iir.rs @@ -13,7 +13,7 @@ use hardware::{ InputPin, AFE0, AFE1, }; -use net::{Action, MiniconfInterface}; +use net::{Action, MqttInterface}; const SCALE: f32 = i16::MAX as _; @@ -46,7 +46,7 @@ const APP: () = { digital_input1: DigitalInput1, adcs: (Adc0Input, Adc1Input), dacs: (Dac0Output, Dac1Output), - mqtt_config: MiniconfInterface, + mqtt: MqttInterface, // Format: iir_state[ch][cascade-no][coeff] #[init([[[0.; 5]; IIR_CASCADE_LENGTH]; 2])] @@ -59,7 +59,7 @@ const APP: () = { // Configure the microcontroller let (mut stabilizer, _pounder) = hardware::setup(c.core, c.device); - let mqtt_config = MiniconfInterface::new( + let mqtt = MqttInterface::new( stabilizer.net.stack, "", &net::get_device_prefix( @@ -86,7 +86,7 @@ const APP: () = { afes: stabilizer.afes, adcs: stabilizer.adcs, dacs: stabilizer.dacs, - mqtt_config, + mqtt, digital_input1: stabilizer.digital_inputs.1, settings: Settings::default(), } @@ -143,14 +143,10 @@ const APP: () = { } } - #[idle(resources=[mqtt_config], spawn=[settings_update])] + #[idle(resources=[mqtt], spawn=[settings_update])] fn idle(mut c: idle::Context) -> ! { loop { - match c - .resources - .mqtt_config - .lock(|config_interface| config_interface.update()) - { + match c.resources.mqtt.lock(|mqtt| mqtt.update()) { Some(Action::Sleep) => cortex_m::asm::wfi(), Some(Action::UpdateSettings) => { c.spawn.settings_update().unwrap() @@ -160,12 +156,12 @@ const APP: () = { } } - #[task(priority = 1, resources=[mqtt_config, afes, settings])] + #[task(priority = 1, resources=[mqtt, afes, settings])] fn settings_update(mut c: settings_update::Context) { - let settings = &c.resources.mqtt_config.mqtt.settings; + let settings = c.resources.mqtt.settings(); // Update the IIR channels. - c.resources.settings.lock(|current| *current = *settings); + c.resources.settings.lock(|current| *current = settings); // Update AFEs c.resources.afes.0.set_gain(settings.afe[0]); diff --git a/src/bin/lockin-external.rs b/src/bin/lockin-external.rs index 082b0d2..f9e9ae4 100644 --- a/src/bin/lockin-external.rs +++ b/src/bin/lockin-external.rs @@ -16,7 +16,7 @@ use stabilizer::hardware::{ }; use miniconf::Miniconf; -use stabilizer::net::{Action, MiniconfInterface}; +use stabilizer::net::{Action, MqttInterface}; #[derive(Copy, Clone, Debug, Deserialize, Miniconf)] enum Conf { @@ -60,7 +60,7 @@ const APP: () = { afes: (AFE0, AFE1), adcs: (Adc0Input, Adc1Input), dacs: (Dac0Output, Dac1Output), - mqtt_config: MiniconfInterface, + mqtt: MqttInterface, settings: Settings, timestamper: InputStamper, @@ -73,7 +73,7 @@ const APP: () = { // Configure the microcontroller let (mut stabilizer, _pounder) = setup(c.core, c.device); - let mqtt_config = MiniconfInterface::new( + let mqtt = MqttInterface::new( stabilizer.net.stack, "", &net::get_device_prefix( @@ -113,7 +113,7 @@ const APP: () = { afes: stabilizer.afes, adcs: stabilizer.adcs, dacs: stabilizer.dacs, - mqtt_config, + mqtt, timestamper: stabilizer.timestamper, settings, @@ -195,14 +195,10 @@ const APP: () = { } } - #[idle(resources=[mqtt_config], spawn=[settings_update])] + #[idle(resources=[mqtt], spawn=[settings_update])] fn idle(mut c: idle::Context) -> ! { loop { - match c - .resources - .mqtt_config - .lock(|config_interface| config_interface.update()) - { + match c.resources.mqtt.lock(|mqtt| mqtt.update()) { Some(Action::Sleep) => cortex_m::asm::wfi(), Some(Action::UpdateSettings) => { c.spawn.settings_update().unwrap() @@ -212,14 +208,14 @@ const APP: () = { } } - #[task(priority = 1, resources=[mqtt_config, settings, afes])] + #[task(priority = 1, resources=[mqtt, settings, afes])] fn settings_update(mut c: settings_update::Context) { - let settings = &c.resources.mqtt_config.mqtt.settings; + let settings = c.resources.mqtt.settings(); c.resources.afes.0.set_gain(settings.afe[0]); c.resources.afes.1.set_gain(settings.afe[1]); - c.resources.settings.lock(|current| *current = *settings); + c.resources.settings.lock(|current| *current = settings); } #[task(binds = ETH, priority = 1)] diff --git a/src/net/mod.rs b/src/net/mod.rs index 13b514c..e2398b8 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -2,10 +2,10 @@ use crate::hardware::{ design_parameters::MQTT_BROKER, CycleCounter, EthernetPhy, NetworkStack, }; -use core::fmt::Write; +use core::{cell::RefCell, fmt::Write}; -use heapless::{consts, String}; -use miniconf::minimq; +use heapless::{consts, String, Vec}; +use serde::Serialize; /// Potential actions for firmware to take. pub enum Action { @@ -17,19 +17,22 @@ pub enum Action { } /// MQTT settings interface. -pub struct MiniconfInterface +pub struct MqttInterface where - S: miniconf::Miniconf + Default, + S: miniconf::Miniconf + Default + Clone, { - pub mqtt: miniconf::MqttInterface, + telemetry_topic: String, + mqtt: RefCell>, + miniconf: RefCell>, clock: CycleCounter, phy: EthernetPhy, network_was_reset: bool, + subscribed: bool, } -impl MiniconfInterface +impl MqttInterface where - S: miniconf::Miniconf + Default, + S: miniconf::Miniconf + Default + Clone, { /// Construct a new MQTT settings interface. /// @@ -46,21 +49,23 @@ where phy: EthernetPhy, clock: CycleCounter, ) -> Self { - let mqtt = { - let mqtt_client = { - minimq::MqttClient::new(MQTT_BROKER.into(), client_id, stack) - .unwrap() - }; + let mqtt_client = + minimq::MqttClient::new(MQTT_BROKER.into(), client_id, stack) + .unwrap(); + let config = + miniconf::MiniconfInterface::new(prefix, S::default()).unwrap(); - miniconf::MqttInterface::new(mqtt_client, prefix, S::default()) - .unwrap() - }; + let mut telemetry_topic: String = String::new(); + write!(&mut telemetry_topic, "{}/telemetry", prefix).unwrap(); Self { - mqtt, + mqtt: RefCell::new(mqtt_client), + miniconf: RefCell::new(config), clock, phy, + telemetry_topic, network_was_reset: false, + subscribed: false, } } @@ -72,7 +77,7 @@ where let now = self.clock.current_ms(); // First, service the network stack to process and inbound and outbound traffic. - let sleep = match self.mqtt.network_stack().poll(now) { + let sleep = match self.mqtt.borrow_mut().network_stack.poll(now) { Ok(updated) => !updated, Err(err) => { log::info!("Network error: {:?}", err); @@ -87,19 +92,93 @@ where // sending an excessive number of DHCP requests. if !self.network_was_reset { self.network_was_reset = true; - self.mqtt.network_stack().handle_link_reset(); + self.mqtt.borrow_mut().network_stack.handle_link_reset(); } } else { self.network_was_reset = false; } - // Finally, service the MQTT interface and handle any necessary messages. - match self.mqtt.update() { - Ok(true) => Some(Action::UpdateSettings), - Ok(false) if sleep => Some(Action::Sleep), - Ok(_) => None, + // If we're no longer subscribed to the settings topic, but we are connected to the broker, + // resubscribe. + if !self.subscribed && self.mqtt.borrow_mut().is_connected().unwrap() { + self.mqtt + .borrow_mut() + .subscribe( + self.miniconf.borrow_mut().get_listening_topic(), + &[], + ) + .unwrap(); + self.subscribed = true; + } - Err(miniconf::MqttError::Network( + let mut update = false; + + // Handle any MQTT traffic. + match self.mqtt.borrow_mut().poll( + |client, topic, message, properties| { + // Find correlation-data and response topics. + let correlation_data = properties.iter().find_map(|prop| { + if let minimq::Property::CorrelationData(data) = prop { + Some(*data) + } else { + None + } + }); + let response_topic = properties.iter().find_map(|prop| { + if let minimq::Property::ResponseTopic(topic) = prop { + Some(*topic) + } else { + None + } + }); + + let incoming = miniconf::Message { + data: message, + correlation_data, + response_topic, + }; + + if let Some(response) = + self.miniconf.borrow_mut().process(topic, incoming) + { + let mut response_properties: Vec< + minimq::Property, + consts::U1, + > = Vec::new(); + if let Some(data) = response.correlation_data { + response_properties + .push(minimq::Property::CorrelationData(data)) + .unwrap(); + } + + // Make a best-effort attempt to send the response. + client + .publish( + response.topic, + &response.data.into_bytes(), + minimq::QoS::AtMostOnce, + &response_properties, + ) + .ok(); + update = true; + } + }, + ) { + // If settings updated, + Ok(_) => { + if update { + Some(Action::UpdateSettings) + } else if sleep { + Some(Action::Sleep) + } else { + None + } + } + Err(minimq::Error::Disconnected) => { + self.subscribed = false; + None + } + Err(minimq::Error::Network( smoltcp_nal::NetworkError::NoIpAddress, )) => None, @@ -109,6 +188,25 @@ where } } } + + pub fn publish_telemetry(&mut self, telemetry: &impl Serialize) { + let telemetry = + miniconf::serde_json_core::to_string::(telemetry) + .unwrap(); + self.mqtt + .borrow_mut() + .publish( + &self.telemetry_topic, + telemetry.as_bytes(), + minimq::QoS::AtMostOnce, + &[], + ) + .ok(); + } + + pub fn settings(&self) -> S { + self.miniconf.borrow().settings.clone() + } } /// Get the MQTT prefix of a device. From f38e3b96084c1fadf3459fdd212cf33e5e7c899b Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Thu, 22 Apr 2021 15:57:24 +0200 Subject: [PATCH 06/12] Simplifying MQTT handling --- Cargo.lock | 3 +-- Cargo.toml | 11 ++++++++--- src/net/mod.rs | 29 ++++------------------------- 3 files changed, 13 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ff026ff..13c7828 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -203,7 +203,6 @@ dependencies = [ [[package]] name = "derive_miniconf" version = "0.1.0" -source = "git+https://github.com/quartiq/miniconf.git?branch=feature/mqtt-removal#6b6b9d7973ac5b4771c211c775c51f82d4a7727f" dependencies = [ "proc-macro2", "quote", @@ -416,10 +415,10 @@ dependencies = [ [[package]] name = "miniconf" version = "0.1.0" -source = "git+https://github.com/quartiq/miniconf.git?branch=feature/mqtt-removal#6b6b9d7973ac5b4771c211c775c51f82d4a7727f" dependencies = [ "derive_miniconf", "heapless 0.6.1", + "minimq", "serde", "serde-json-core", ] diff --git a/Cargo.toml b/Cargo.toml index db89ef5..00ba5c6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,7 +44,7 @@ paste = "1" dsp = { path = "dsp" } ad9959 = { path = "ad9959" } generic-array = "0.14" -miniconf = "0.1.0" +miniconf = { version = "0.1.0", features = ["minimq-support"] } [dependencies.mcp23017] git = "https://github.com/mrd0ll4r/mcp23017.git" @@ -55,8 +55,13 @@ features = ["stm32h743v", "rt", "unproven", "ethernet", "quadspi"] version = "0.9.0" [patch.crates-io.miniconf] -git = "https://github.com/quartiq/miniconf.git" -branch = "feature/mqtt-removal" +path = "../miniconf" +# git = "https://github.com/quartiq/miniconf.git" +# branch = "feature/mqtt-removal" + +[patch.crates-io.minimq] +git = "https://github.com/quartiq/minimq.git" +rev = "933687c2e4b" [dependencies.smoltcp-nal] git = "https://github.com/quartiq/smoltcp-nal.git" diff --git a/src/net/mod.rs b/src/net/mod.rs index e2398b8..de55d1a 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -116,31 +116,10 @@ where // Handle any MQTT traffic. match self.mqtt.borrow_mut().poll( |client, topic, message, properties| { - // Find correlation-data and response topics. - let correlation_data = properties.iter().find_map(|prop| { - if let minimq::Property::CorrelationData(data) = prop { - Some(*data) - } else { - None - } - }); - let response_topic = properties.iter().find_map(|prop| { - if let minimq::Property::ResponseTopic(topic) = prop { - Some(*topic) - } else { - None - } - }); - - let incoming = miniconf::Message { - data: message, - correlation_data, - response_topic, - }; - - if let Some(response) = - self.miniconf.borrow_mut().process(topic, incoming) - { + if let Some(response) = self.miniconf.borrow_mut().process( + topic, + miniconf::Message::from(message, properties), + ) { let mut response_properties: Vec< minimq::Property, consts::U1, From 61dc6e340eced121ff499ad456f5450a1df1e47d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 28 Apr 2021 16:27:59 +0200 Subject: [PATCH 07/12] dual-iir: add some mqtt parameter docs --- src/bin/dual-iir.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/bin/dual-iir.rs b/src/bin/dual-iir.rs index eaa82ae..6d12be9 100644 --- a/src/bin/dual-iir.rs +++ b/src/bin/dual-iir.rs @@ -31,9 +31,17 @@ pub struct Settings { impl Default for Settings { fn default() -> Self { Self { + // Analog frontend programmable gain amplifier gains (G1, G2, G5, G10) afe: [AfeGain::G1, AfeGain::G1], + // IIR filter tap gains are an array `[b0, b1, b2, a1, a2]` such that the + // new output is computed as `y0 = a1*y1 + a2*y2 + b0*x0 + b1*x1 + b2*x2`. + // The array is `iir_state[channel-index][cascade-index][coeff-index]`. + // The IIR coefficients can be mapped to other transfer function + // representations, for example as described in https://arxiv.org/abs/1508.06319 iir_ch: [[iir::IIR::new(1., -SCALE, SCALE); IIR_CASCADE_LENGTH]; 2], + // Permit the DI1 digital input to suppress filter output updates. allow_hold: false, + // Force suppress filter output updates. force_hold: false, } } @@ -48,7 +56,6 @@ const APP: () = { dacs: (Dac0Output, Dac1Output), mqtt_config: MiniconfInterface, - // Format: iir_state[ch][cascade-no][coeff] #[init([[[0.; 5]; IIR_CASCADE_LENGTH]; 2])] iir_state: [[iir::Vec5; IIR_CASCADE_LENGTH]; 2], settings: Settings, From 0922cc42af194d79a0c8fc88f2fa2836fe10b3d4 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Wed, 28 Apr 2021 21:03:38 +0200 Subject: [PATCH 08/12] Adding new miniconf implementation --- Cargo.lock | 16 ++- Cargo.toml | 17 ++- src/net/mod.rs | 184 ++------------------------------- src/net/mqtt_interface.rs | 212 ++++++++++++++++++++++++++++++++++++++ src/net/router.rs | 91 ++++++++++++++++ 5 files changed, 327 insertions(+), 193 deletions(-) create mode 100644 src/net/mqtt_interface.rs create mode 100644 src/net/router.rs diff --git a/Cargo.lock b/Cargo.lock index 13c7828..9007083 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -203,6 +203,7 @@ dependencies = [ [[package]] name = "derive_miniconf" version = "0.1.0" +source = "git+https://github.com/quartiq/miniconf.git?rev=c8d819c#c8d819cdab65f18396e65dafa3558daea29e3031" dependencies = [ "proc-macro2", "quote", @@ -415,10 +416,10 @@ dependencies = [ [[package]] name = "miniconf" version = "0.1.0" +source = "git+https://github.com/quartiq/miniconf.git?rev=c8d819c#c8d819cdab65f18396e65dafa3558daea29e3031" dependencies = [ "derive_miniconf", "heapless 0.6.1", - "minimq", "serde", "serde-json-core", ] @@ -426,7 +427,7 @@ dependencies = [ [[package]] name = "minimq" version = "0.2.0" -source = "git+https://github.com/quartiq/minimq.git?rev=933687c2e4b#933687c2e4bc8a4d972de9a4d1508b0b554a8b38" +source = "git+https://github.com/quartiq/minimq.git?branch=rs/issue-40/copyable-properties#c95c758b620ee98752852bb643df8557a7200f3f" dependencies = [ "bit_field", "embedded-nal", @@ -656,6 +657,12 @@ dependencies = [ "semver", ] +[[package]] +name = "ryu" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" + [[package]] name = "semver" version = "0.9.0" @@ -683,9 +690,10 @@ dependencies = [ [[package]] name = "serde-json-core" version = "0.2.0" -source = "git+https://github.com/rust-embedded-community/serde-json-core.git?rev=ee06ac91bc#ee06ac91bc43b72450a92198a00d9e5c5b9946d2" +source = "git+https://github.com/quartiq/serde-json-core.git?branch=feature/dependency-update#a304506a1efb4a90a6ef3faf71ec3ef5f8433fb4" dependencies = [ - "heapless 0.5.6", + "heapless 0.6.1", + "ryu", "serde", ] diff --git a/Cargo.toml b/Cargo.toml index 00ba5c6..29480d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,7 +44,7 @@ paste = "1" dsp = { path = "dsp" } ad9959 = { path = "ad9959" } generic-array = "0.14" -miniconf = { version = "0.1.0", features = ["minimq-support"] } +miniconf = "0.1.0" [dependencies.mcp23017] git = "https://github.com/mrd0ll4r/mcp23017.git" @@ -55,13 +55,8 @@ features = ["stm32h743v", "rt", "unproven", "ethernet", "quadspi"] version = "0.9.0" [patch.crates-io.miniconf] -path = "../miniconf" -# git = "https://github.com/quartiq/miniconf.git" -# branch = "feature/mqtt-removal" - -[patch.crates-io.minimq] -git = "https://github.com/quartiq/minimq.git" -rev = "933687c2e4b" +git = "https://github.com/quartiq/miniconf.git" +rev = "c8d819c" [dependencies.smoltcp-nal] git = "https://github.com/quartiq/smoltcp-nal.git" @@ -69,11 +64,11 @@ rev = "8468f11" [dependencies.minimq] git = "https://github.com/quartiq/minimq.git" -rev = "933687c2e4b" +branch = "rs/issue-40/copyable-properties" [patch.crates-io.serde-json-core] -git = "https://github.com/rust-embedded-community/serde-json-core.git" -rev = "ee06ac91bc" +git = "https://github.com/quartiq/serde-json-core.git" +branch = "feature/dependency-update" [features] semihosting = ["panic-semihosting", "cortex-m-log/semihosting"] diff --git a/src/net/mod.rs b/src/net/mod.rs index de55d1a..a1e1399 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -1,11 +1,11 @@ -use crate::hardware::{ - design_parameters::MQTT_BROKER, CycleCounter, EthernetPhy, NetworkStack, -}; +use heapless::{consts, String}; -use core::{cell::RefCell, fmt::Write}; +use core::fmt::Write; -use heapless::{consts, String, Vec}; -use serde::Serialize; +mod mqtt_interface; +mod router; +pub use mqtt_interface::MqttInterface; +use router::{RouteResult, SettingsResponse}; /// Potential actions for firmware to take. pub enum Action { @@ -16,178 +16,6 @@ pub enum Action { UpdateSettings, } -/// MQTT settings interface. -pub struct MqttInterface -where - S: miniconf::Miniconf + Default + Clone, -{ - telemetry_topic: String, - mqtt: RefCell>, - miniconf: RefCell>, - clock: CycleCounter, - phy: EthernetPhy, - network_was_reset: bool, - subscribed: bool, -} - -impl MqttInterface -where - S: miniconf::Miniconf + Default + Clone, -{ - /// Construct a new MQTT settings interface. - /// - /// # Args - /// * `stack` - The network stack to use for communication. - /// * `client_id` - The ID of the MQTT client. May be an empty string for auto-assigning. - /// * `prefix` - The MQTT device prefix to use for this device. - /// * `phy` - The PHY driver for querying the link state. - /// * `clock` - The clock to utilize for querying the current system time. - pub fn new( - stack: NetworkStack, - client_id: &str, - prefix: &str, - phy: EthernetPhy, - clock: CycleCounter, - ) -> Self { - let mqtt_client = - minimq::MqttClient::new(MQTT_BROKER.into(), client_id, stack) - .unwrap(); - let config = - miniconf::MiniconfInterface::new(prefix, S::default()).unwrap(); - - let mut telemetry_topic: String = String::new(); - write!(&mut telemetry_topic, "{}/telemetry", prefix).unwrap(); - - Self { - mqtt: RefCell::new(mqtt_client), - miniconf: RefCell::new(config), - clock, - phy, - telemetry_topic, - network_was_reset: false, - subscribed: false, - } - } - - /// Update the MQTT interface and service the network - /// - /// # Returns - /// An option containing an action that should be completed as a result of network servicing. - pub fn update(&mut self) -> Option { - let now = self.clock.current_ms(); - - // First, service the network stack to process and inbound and outbound traffic. - let sleep = match self.mqtt.borrow_mut().network_stack.poll(now) { - Ok(updated) => !updated, - Err(err) => { - log::info!("Network error: {:?}", err); - false - } - }; - - // If the PHY indicates there's no more ethernet link, reset the DHCP server in the network - // stack. - if self.phy.poll_link() == false { - // Only reset the network stack once per link reconnection. This prevents us from - // sending an excessive number of DHCP requests. - if !self.network_was_reset { - self.network_was_reset = true; - self.mqtt.borrow_mut().network_stack.handle_link_reset(); - } - } else { - self.network_was_reset = false; - } - - // If we're no longer subscribed to the settings topic, but we are connected to the broker, - // resubscribe. - if !self.subscribed && self.mqtt.borrow_mut().is_connected().unwrap() { - self.mqtt - .borrow_mut() - .subscribe( - self.miniconf.borrow_mut().get_listening_topic(), - &[], - ) - .unwrap(); - self.subscribed = true; - } - - let mut update = false; - - // Handle any MQTT traffic. - match self.mqtt.borrow_mut().poll( - |client, topic, message, properties| { - if let Some(response) = self.miniconf.borrow_mut().process( - topic, - miniconf::Message::from(message, properties), - ) { - let mut response_properties: Vec< - minimq::Property, - consts::U1, - > = Vec::new(); - if let Some(data) = response.correlation_data { - response_properties - .push(minimq::Property::CorrelationData(data)) - .unwrap(); - } - - // Make a best-effort attempt to send the response. - client - .publish( - response.topic, - &response.data.into_bytes(), - minimq::QoS::AtMostOnce, - &response_properties, - ) - .ok(); - update = true; - } - }, - ) { - // If settings updated, - Ok(_) => { - if update { - Some(Action::UpdateSettings) - } else if sleep { - Some(Action::Sleep) - } else { - None - } - } - Err(minimq::Error::Disconnected) => { - self.subscribed = false; - None - } - Err(minimq::Error::Network( - smoltcp_nal::NetworkError::NoIpAddress, - )) => None, - - Err(error) => { - log::info!("Unexpected error: {:?}", error); - None - } - } - } - - pub fn publish_telemetry(&mut self, telemetry: &impl Serialize) { - let telemetry = - miniconf::serde_json_core::to_string::(telemetry) - .unwrap(); - self.mqtt - .borrow_mut() - .publish( - &self.telemetry_topic, - telemetry.as_bytes(), - minimq::QoS::AtMostOnce, - &[], - ) - .ok(); - } - - pub fn settings(&self) -> S { - self.miniconf.borrow().settings.clone() - } -} - /// Get the MQTT prefix of a device. /// /// # Args diff --git a/src/net/mqtt_interface.rs b/src/net/mqtt_interface.rs new file mode 100644 index 0000000..bcc6ae7 --- /dev/null +++ b/src/net/mqtt_interface.rs @@ -0,0 +1,212 @@ +use crate::hardware::{ + design_parameters::MQTT_BROKER, CycleCounter, EthernetPhy, NetworkStack, +}; + +use core::{cell::RefCell, fmt::Write}; + +use heapless::{consts, String}; +use serde::Serialize; + +use super::{Action, RouteResult, SettingsResponse}; + +/// MQTT settings interface. +pub struct MqttInterface +where + S: miniconf::Miniconf + Default + Clone, +{ + telemetry_topic: String, + default_response_topic: String, + mqtt: RefCell>, + settings: RefCell, + clock: CycleCounter, + phy: EthernetPhy, + network_was_reset: bool, + subscribed: bool, + id: String, +} + +impl MqttInterface +where + S: miniconf::Miniconf + Default + Clone, +{ + /// Construct a new MQTT settings interface. + /// + /// # Args + /// * `stack` - The network stack to use for communication. + /// * `client_id` - The ID of the MQTT client. May be an empty string for auto-assigning. + /// * `prefix` - The MQTT device prefix to use for this device. + /// * `phy` - The PHY driver for querying the link state. + /// * `clock` - The clock to utilize for querying the current system time. + pub fn new( + stack: NetworkStack, + client_id: &str, + prefix: &str, + phy: EthernetPhy, + clock: CycleCounter, + ) -> Self { + let mqtt_client = + minimq::MqttClient::new(MQTT_BROKER.into(), client_id, stack) + .unwrap(); + + let mut telemetry_topic: String = String::new(); + write!(&mut telemetry_topic, "{}/telemetry", prefix).unwrap(); + + let mut response_topic: String = String::new(); + write!(&mut response_topic, "{}/log", prefix).unwrap(); + + Self { + mqtt: RefCell::new(mqtt_client), + settings: RefCell::new(S::default()), + id: String::from(prefix), + clock, + phy, + telemetry_topic, + default_response_topic: response_topic, + network_was_reset: false, + subscribed: false, + } + } + + /// Update the MQTT interface and service the network + /// + /// # Returns + /// An option containing an action that should be completed as a result of network servicing. + pub fn update(&mut self) -> Option { + // First, service the network stack to process any inbound and outbound traffic. + let sleep = match self + .mqtt + .borrow_mut() + .network_stack + .poll(self.clock.current_ms()) + { + Ok(updated) => !updated, + Err(err) => { + log::info!("Network error: {:?}", err); + false + } + }; + + // If the PHY indicates there's no more ethernet link, reset the DHCP server in the network + // stack. + if self.phy.poll_link() == false { + // Only reset the network stack once per link reconnection. This prevents us from + // sending an excessive number of DHCP requests. + if !self.network_was_reset { + self.network_was_reset = true; + self.mqtt.borrow_mut().network_stack.handle_link_reset(); + } + } else { + self.network_was_reset = false; + } + + // If we're no longer subscribed to the settings topic, but we are connected to the broker, + // resubscribe. + if !self.subscribed && self.mqtt.borrow_mut().is_connected().unwrap() { + let mut settings_topic: String = String::new(); + write!(&mut settings_topic, "{}/settings/#", self.id.as_str()) + .unwrap(); + + self.mqtt + .borrow_mut() + .subscribe(&settings_topic, &[]) + .unwrap(); + self.subscribed = true; + } + + // Handle any MQTT traffic. + let mut update = false; + match self.mqtt.borrow_mut().poll( + |client, topic, message, properties| { + let (response, settings_update) = + self.route_message(topic, message, properties); + client + .publish( + response.response_topic, + &response.message, + minimq::QoS::AtMostOnce, + &response.properties, + ) + .ok(); + update = settings_update; + }, + ) { + // If settings updated, + Ok(_) => { + if update { + Some(Action::UpdateSettings) + } else if sleep { + Some(Action::Sleep) + } else { + None + } + } + Err(minimq::Error::Disconnected) => { + self.subscribed = false; + None + } + Err(minimq::Error::Network( + smoltcp_nal::NetworkError::NoIpAddress, + )) => None, + + Err(error) => { + log::info!("Unexpected error: {:?}", error); + None + } + } + } + + fn route_message<'a, 'me: 'a>( + &'me self, + topic: &str, + message: &[u8], + properties: &[minimq::Property<'a>], + ) -> (RouteResult<'a>, bool) { + let mut response = + RouteResult::new(properties, &self.default_response_topic); + let mut update = false; + + if let Some(path) = topic.strip_prefix(self.id.as_str()) { + let mut parts = path[1..].split('/'); + match parts.next() { + Some("settings") => { + let result = self + .settings + .borrow_mut() + .string_set(parts.peekable(), message); + update = result.is_ok(); + response.set_message(SettingsResponse::new(result, topic)); + } + Some(_) => response.set_message(SettingsResponse::custom( + "Unknown topic", + 255, + )), + _ => response + .set_message(SettingsResponse::custom("No topic", 254)), + } + } else { + response + .set_message(SettingsResponse::custom("Invalid prefix", 253)); + } + + (response, update) + } + + pub fn publish_telemetry(&mut self, telemetry: &impl Serialize) { + let telemetry = + miniconf::serde_json_core::to_string::(telemetry) + .unwrap(); + self.mqtt + .borrow_mut() + .publish( + &self.telemetry_topic, + telemetry.as_bytes(), + minimq::QoS::AtMostOnce, + &[], + ) + .ok(); + } + + pub fn settings(&self) -> S { + self.settings.borrow().clone() + } +} diff --git a/src/net/router.rs b/src/net/router.rs new file mode 100644 index 0000000..c8b7776 --- /dev/null +++ b/src/net/router.rs @@ -0,0 +1,91 @@ +use heapless::{consts, String, Vec}; +use serde::Serialize; + +use core::fmt::Write; + +pub struct RouteResult<'a> { + pub response_topic: &'a str, + pub message: Vec, + pub properties: Vec, consts::U1>, +} + +#[derive(Serialize)] +pub struct SettingsResponse { + code: u8, + msg: String, +} + +impl<'a> RouteResult<'a> { + pub fn new<'b: 'a>( + properties: &[minimq::Property<'a>], + default_response: &'b str, + ) -> Self { + // Extract the MQTT response topic. + let response_topic = properties + .iter() + .find_map(|prop| { + if let minimq::Property::ResponseTopic(topic) = prop { + Some(topic) + } else { + None + } + }) + .unwrap_or(&default_response); + + // Associate any provided correlation data with the response. + let mut correlation_data: Vec, consts::U1> = + Vec::new(); + if let Some(data) = properties + .iter() + .find(|prop| matches!(prop, minimq::Property::CorrelationData(_))) + { + // Note(unwrap): Unwrap can not fail, as we only ever push one value. + correlation_data.push(*data).unwrap(); + } + + RouteResult { + response_topic, + message: Vec::new(), + properties: correlation_data, + } + } + + pub fn set_message(&mut self, response: impl Serialize) { + self.message = miniconf::serde_json_core::to_vec(&response).unwrap(); + } +} + +impl SettingsResponse { + pub fn new(result: Result<(), miniconf::Error>, path: &str) -> Self { + match result { + Ok(_) => { + let mut msg: String = String::new(); + if write!(&mut msg, "{} updated", path).is_err() { + msg = String::from("Latest update succeeded"); + } + + Self { msg, code: 0 } + } + Err(error) => { + let mut msg: String = String::new(); + if write!(&mut msg, "{} update failed: {:?}", path, error) + .is_err() + { + if write!(&mut msg, "Latest update failed: {:?}", error) + .is_err() + { + msg = String::from("Latest update failed"); + } + } + Self { msg, code: 5 } + } + } + } + + pub fn custom(msg: &str, code: u8) -> Self { + Self { + code, + msg: String::from(msg), + } + } +} From aad026161f97aedb103f718a37fe6ceb84e5d060 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Thu, 29 Apr 2021 11:28:35 +0200 Subject: [PATCH 09/12] Refactoring to support DHCP --- Cargo.lock | 4 ++-- Cargo.toml | 6 +++--- src/net/mqtt_interface.rs | 19 +++++++++++++++++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a002739..aef5a92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -427,7 +427,7 @@ dependencies = [ [[package]] name = "minimq" version = "0.2.0" -source = "git+https://github.com/quartiq/minimq.git?branch=rs/issue-40/copyable-properties#c95c758b620ee98752852bb643df8557a7200f3f" +source = "git+https://github.com/quartiq/minimq.git?rev=b3f364d#b3f364d55dea35da6572f78ddb91c87bfbb453bf" dependencies = [ "bit_field", "embedded-nal", @@ -690,7 +690,7 @@ dependencies = [ [[package]] name = "serde-json-core" version = "0.2.0" -source = "git+https://github.com/quartiq/serde-json-core.git?branch=feature/dependency-update#a304506a1efb4a90a6ef3faf71ec3ef5f8433fb4" +source = "git+https://github.com/rust-embedded-community/serde-json-core.git?rev=da460d1#da460d123e217f0e822a3977eb2170ed5d279d5e" dependencies = [ "heapless 0.6.1", "ryu", diff --git a/Cargo.toml b/Cargo.toml index 095fb74..4ab885e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,11 +64,11 @@ rev = "8468f11" [dependencies.minimq] git = "https://github.com/quartiq/minimq.git" -branch = "rs/issue-40/copyable-properties" +rev = "b3f364d" [patch.crates-io.serde-json-core] -git = "https://github.com/quartiq/serde-json-core.git" -branch = "feature/dependency-update" +git = "https://github.com/rust-embedded-community/serde-json-core.git" +rev = "da460d1" [features] semihosting = ["panic-semihosting", "cortex-m-log/semihosting"] diff --git a/src/net/mqtt_interface.rs b/src/net/mqtt_interface.rs index bcc6ae7..374714d 100644 --- a/src/net/mqtt_interface.rs +++ b/src/net/mqtt_interface.rs @@ -22,7 +22,7 @@ where phy: EthernetPhy, network_was_reset: bool, subscribed: bool, - id: String, + id: String, } impl MqttInterface @@ -99,9 +99,24 @@ where self.network_was_reset = false; } + let mqtt_connected = match self.mqtt.borrow_mut().is_connected() { + Ok(connected) => connected, + Err(minimq::Error::Network( + smoltcp_nal::NetworkError::NoIpAddress, + )) => false, + Err(minimq::Error::Network(error)) => { + log::info!("Unexpected network error: {:?}", error); + false + } + Err(error) => { + log::warn!("Unexpected MQTT error: {:?}", error); + false + } + }; + // If we're no longer subscribed to the settings topic, but we are connected to the broker, // resubscribe. - if !self.subscribed && self.mqtt.borrow_mut().is_connected().unwrap() { + if !self.subscribed && mqtt_connected { let mut settings_topic: String = String::new(); write!(&mut settings_topic, "{}/settings/#", self.id.as_str()) .unwrap(); From 7ddf2271f3779dddbf892c42a560c0fa9f975105 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Thu, 29 Apr 2021 11:54:16 +0200 Subject: [PATCH 10/12] Refactoring net module --- src/net/messages.rs | 124 ++++++++++++++++++++++++++++++++++++++ src/net/mod.rs | 11 +++- src/net/mqtt_interface.rs | 62 ++++++++++--------- src/net/router.rs | 91 ---------------------------- 4 files changed, 168 insertions(+), 120 deletions(-) create mode 100644 src/net/messages.rs delete mode 100644 src/net/router.rs diff --git a/src/net/messages.rs b/src/net/messages.rs new file mode 100644 index 0000000..2f54f52 --- /dev/null +++ b/src/net/messages.rs @@ -0,0 +1,124 @@ +use heapless::{consts, String, Vec}; +use serde::Serialize; + +use core::fmt::Write; + +#[derive(Debug, Copy, Clone)] +pub enum SettingsResponseCode { + NoError = 0, + NoTopic = 1, + InvalidPrefix = 2, + UnknownTopic = 3, + UpdateFailure = 4, +} + +/// Represents a generic MQTT message. +pub struct MqttMessage<'a> { + pub topic: &'a str, + pub message: Vec, + pub properties: Vec, consts::U1>, +} + +/// The payload of the MQTT response message to a settings update request. +#[derive(Serialize)] +pub struct SettingsResponse { + code: u8, + msg: String, +} + +impl<'a> MqttMessage<'a> { + /// Construct a new MQTT message from an incoming message. + /// + /// # Args + /// * `properties` - A list of properties associated with the inbound message. + /// * `default_response` - The default response topic for the message + /// * `msg` - The response associated with the message. Must fit within 128 bytes. + pub fn new<'b: 'a>( + properties: &[minimq::Property<'a>], + default_response: &'b str, + msg: &impl Serialize, + ) -> Self { + // Extract the MQTT response topic. + let topic = properties + .iter() + .find_map(|prop| { + if let minimq::Property::ResponseTopic(topic) = prop { + Some(topic) + } else { + None + } + }) + .unwrap_or(&default_response); + + // Associate any provided correlation data with the response. + let mut correlation_data: Vec, consts::U1> = + Vec::new(); + if let Some(data) = properties + .iter() + .find(|prop| matches!(prop, minimq::Property::CorrelationData(_))) + { + // Note(unwrap): Unwrap can not fail, as we only ever push one value. + correlation_data.push(*data).unwrap(); + } + + Self { + topic, + // Note(unwrap): All SettingsResponse objects are guaranteed to fit in the vector. + message: miniconf::serde_json_core::to_vec(msg).unwrap(), + properties: correlation_data, + } + } +} + +impl SettingsResponse { + /// Construct a settings response upon successful settings update. + /// + /// # Args + /// * `path` - The path of the setting that was updated. + pub fn update_success(path: &str) -> Self { + let mut msg: String = String::new(); + if write!(&mut msg, "{} updated", path).is_err() { + msg = String::from("Latest update succeeded"); + } + + Self { + msg, + code: SettingsResponseCode::NoError as u8, + } + } + + /// Construct a response when a settings update failed. + /// + /// # Args + /// * `path` - The settings path that configuration failed for. + /// * `err` - The settings update error that occurred. + pub fn update_failure(path: &str, err: miniconf::Error) -> Self { + let mut msg: String = String::new(); + if write!(&mut msg, "{} update failed: {:?}", path, err).is_err() { + if write!(&mut msg, "Latest update failed: {:?}", err).is_err() { + msg = String::from("Latest update failed"); + } + } + + Self { + msg, + code: SettingsResponseCode::UpdateFailure as u8, + } + } + + /// Construct a response from a custom response code. + /// + /// # Args + /// * `code` - The response code to provide. + pub fn code(code: SettingsResponseCode) -> Self { + let mut msg: String = String::new(); + + // Note(unwrap): All code debug names shall fit in the 64 byte string. + write!(&mut msg, "{:?}", code).unwrap(); + + Self { + code: code as u8, + msg, + } + } +} diff --git a/src/net/mod.rs b/src/net/mod.rs index a1e1399..9a86b85 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -1,11 +1,18 @@ +///! Stabilizer network management module +///! +///! # Design +///! The stabilizer network architecture supports numerous layers to permit transmission of +///! telemetry (via MQTT), configuration of run-time settings (via MQTT + Miniconf), and live data +///! streaming over raw UDP/TCP sockets. This module encompasses the main processing routines +///! related to Stabilizer networking operations. use heapless::{consts, String}; use core::fmt::Write; +mod messages; mod mqtt_interface; -mod router; +use messages::{MqttMessage, SettingsResponse, SettingsResponseCode}; pub use mqtt_interface::MqttInterface; -use router::{RouteResult, SettingsResponse}; /// Potential actions for firmware to take. pub enum Action { diff --git a/src/net/mqtt_interface.rs b/src/net/mqtt_interface.rs index 374714d..209a67d 100644 --- a/src/net/mqtt_interface.rs +++ b/src/net/mqtt_interface.rs @@ -7,7 +7,7 @@ use core::{cell::RefCell, fmt::Write}; use heapless::{consts, String}; use serde::Serialize; -use super::{Action, RouteResult, SettingsResponse}; +use super::{Action, MqttMessage, SettingsResponse, SettingsResponseCode}; /// MQTT settings interface. pub struct MqttInterface @@ -136,7 +136,7 @@ where self.route_message(topic, message, properties); client .publish( - response.response_topic, + response.topic, &response.message, minimq::QoS::AtMostOnce, &response.properties, @@ -175,33 +175,41 @@ where topic: &str, message: &[u8], properties: &[minimq::Property<'a>], - ) -> (RouteResult<'a>, bool) { - let mut response = - RouteResult::new(properties, &self.default_response_topic); + ) -> (MqttMessage<'a>, bool) { let mut update = false; - - if let Some(path) = topic.strip_prefix(self.id.as_str()) { - let mut parts = path[1..].split('/'); - match parts.next() { - Some("settings") => { - let result = self - .settings - .borrow_mut() - .string_set(parts.peekable(), message); - update = result.is_ok(); - response.set_message(SettingsResponse::new(result, topic)); + let response_msg = + if let Some(path) = topic.strip_prefix(self.id.as_str()) { + let mut parts = path[1..].split('/'); + match parts.next() { + Some("settings") => { + match self + .settings + .borrow_mut() + .string_set(parts.peekable(), message) + { + Ok(_) => { + update = true; + SettingsResponse::update_success(path) + } + Err(error) => { + SettingsResponse::update_failure(path, error) + } + } + } + Some(_) => SettingsResponse::code( + SettingsResponseCode::UnknownTopic, + ), + _ => SettingsResponse::code(SettingsResponseCode::NoTopic), } - Some(_) => response.set_message(SettingsResponse::custom( - "Unknown topic", - 255, - )), - _ => response - .set_message(SettingsResponse::custom("No topic", 254)), - } - } else { - response - .set_message(SettingsResponse::custom("Invalid prefix", 253)); - } + } else { + SettingsResponse::code(SettingsResponseCode::InvalidPrefix) + }; + + let response = MqttMessage::new( + properties, + &self.default_response_topic, + &response_msg, + ); (response, update) } diff --git a/src/net/router.rs b/src/net/router.rs deleted file mode 100644 index c8b7776..0000000 --- a/src/net/router.rs +++ /dev/null @@ -1,91 +0,0 @@ -use heapless::{consts, String, Vec}; -use serde::Serialize; - -use core::fmt::Write; - -pub struct RouteResult<'a> { - pub response_topic: &'a str, - pub message: Vec, - pub properties: Vec, consts::U1>, -} - -#[derive(Serialize)] -pub struct SettingsResponse { - code: u8, - msg: String, -} - -impl<'a> RouteResult<'a> { - pub fn new<'b: 'a>( - properties: &[minimq::Property<'a>], - default_response: &'b str, - ) -> Self { - // Extract the MQTT response topic. - let response_topic = properties - .iter() - .find_map(|prop| { - if let minimq::Property::ResponseTopic(topic) = prop { - Some(topic) - } else { - None - } - }) - .unwrap_or(&default_response); - - // Associate any provided correlation data with the response. - let mut correlation_data: Vec, consts::U1> = - Vec::new(); - if let Some(data) = properties - .iter() - .find(|prop| matches!(prop, minimq::Property::CorrelationData(_))) - { - // Note(unwrap): Unwrap can not fail, as we only ever push one value. - correlation_data.push(*data).unwrap(); - } - - RouteResult { - response_topic, - message: Vec::new(), - properties: correlation_data, - } - } - - pub fn set_message(&mut self, response: impl Serialize) { - self.message = miniconf::serde_json_core::to_vec(&response).unwrap(); - } -} - -impl SettingsResponse { - pub fn new(result: Result<(), miniconf::Error>, path: &str) -> Self { - match result { - Ok(_) => { - let mut msg: String = String::new(); - if write!(&mut msg, "{} updated", path).is_err() { - msg = String::from("Latest update succeeded"); - } - - Self { msg, code: 0 } - } - Err(error) => { - let mut msg: String = String::new(); - if write!(&mut msg, "{} update failed: {:?}", path, error) - .is_err() - { - if write!(&mut msg, "Latest update failed: {:?}", error) - .is_err() - { - msg = String::from("Latest update failed"); - } - } - Self { msg, code: 5 } - } - } - } - - pub fn custom(msg: &str, code: u8) -> Self { - Self { - code, - msg: String::from(msg), - } - } -} From 5499fc6b478d06a9379d75b62b2c19cc45fe0ed6 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Thu, 29 Apr 2021 12:11:35 +0200 Subject: [PATCH 11/12] Updating miniconf to use updated API --- miniconf.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/miniconf.py b/miniconf.py index b059f78..837b2d5 100644 --- a/miniconf.py +++ b/miniconf.py @@ -9,6 +9,7 @@ import argparse import asyncio import json import logging +import sys from gmqtt import Client as MqttClient @@ -51,7 +52,8 @@ class Miniconf: logger.warning('Unexpected response on topic: %s', topic) return - self.inflight[topic].set_result(payload.decode('ascii')) + response = json.loads(payload) + self.inflight[topic].set_result((response['code'], response['msg'])) del self.inflight[topic] async def command(self, path, value): @@ -62,7 +64,8 @@ class Miniconf: value: The value to write to the path. Returns: - The received response to the command. + (code, msg) tuple as a response to the command. `code` is zero for success and `msg` is + a use-readable message indicating further information. """ setting_topic = f'{self.prefix}/settings/{path}' response_topic = f'{self.prefix}/response/{path}' @@ -107,12 +110,17 @@ def main(): async def configure_settings(): interface = await Miniconf.create(args.prefix, args.broker) + failures = 0 for kv in args.settings: path, value = kv.split("=", 1) - response = await interface.command(path, json.loads(value)) - print(response) + code, response = await interface.command(path, json.loads(value)) + if code != 0: + failures += 1 + logging.warning('%s update failed!', path) - loop.run_until_complete(configure_settings()) + return failures + + sys.exit(loop.run_until_complete(configure_settings())) if __name__ == '__main__': From d742350bfdbdb0e46803eca204f89e0805abec16 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Thu, 29 Apr 2021 12:12:46 +0200 Subject: [PATCH 12/12] Updating failure printing --- miniconf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miniconf.py b/miniconf.py index 837b2d5..77e491c 100644 --- a/miniconf.py +++ b/miniconf.py @@ -114,9 +114,9 @@ def main(): for kv in args.settings: path, value = kv.split("=", 1) code, response = await interface.command(path, json.loads(value)) + print(response) if code != 0: failures += 1 - logging.warning('%s update failed!', path) return failures