From e0a5dedee00faa21ce639f42260ac990d622c11f Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Mon, 2 May 2022 12:35:59 +0200 Subject: [PATCH 1/6] Adding metadata topic publish on alive tree --- Cargo.toml | 2 +- src/hardware/metadata.rs | 62 ++++++++++++++++++++++++++++++++++++++++ src/hardware/mod.rs | 10 +++++++ src/hardware/setup.rs | 32 ++++++++++++--------- src/main.rs | 3 +- src/net/mod.rs | 2 ++ src/net/mqtt_control.rs | 57 +++++++++++++++++++++++++++++++----- src/serial_terminal.rs | 39 ++++++++----------------- 8 files changed, 158 insertions(+), 49 deletions(-) create mode 100644 src/hardware/metadata.rs diff --git a/Cargo.toml b/Cargo.toml index 34b9c44b..bb9fa0ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ shared-bus = { version = "0.2", features = ["cortex-m"] } usb-device = "0.2.8" postcard = "0.5.1" crc-any = { version = "2.3.5", default-features = false } -panic-persist = { version = "0.3", features = ["custom-panic-handler"] } +panic-persist = { version = "0.3", features = ["custom-panic-handler", "utf8"] } smoltcp-nal = { version = "0.2" } miniconf = { version = "0.3", features = ["mqtt-client"]} minimq = "0.5.3" diff --git a/src/hardware/metadata.rs b/src/hardware/metadata.rs new file mode 100644 index 00000000..1bcbdd18 --- /dev/null +++ b/src/hardware/metadata.rs @@ -0,0 +1,62 @@ +use serde::Serialize; + +use super::{platform, HardwareVersion}; + +mod build_info { + include!(concat!(env!("OUT_DIR"), "/built.rs")); +} + +static mut METADATA: ApplicationMetadata = ApplicationMetadata { + firmware_version: "Unspecified", + build_time_utc: build_info::BUILT_TIME_UTC, + rust_version: build_info::RUSTC_VERSION, + profile: build_info::PROFILE, + git_revision: "Unspecified", + git_dirty: true, + features: build_info::FEATURES_STR, + panic_info: "None", + watchdog: false, + hardware_version: HardwareVersion::Unknown(0), +}; + +#[derive(Serialize)] +pub struct ApplicationMetadata { + pub firmware_version: &'static str, + pub build_time_utc: &'static str, + pub rust_version: &'static str, + pub git_revision: &'static str, + pub profile: &'static str, + pub git_dirty: bool, + pub features: &'static str, + pub panic_info: &'static str, + pub watchdog: bool, + pub hardware_version: HardwareVersion, +} + +impl ApplicationMetadata { + pub fn new(hardware_version: HardwareVersion) -> &'static ApplicationMetadata { + let mut meta = unsafe { &mut METADATA }; + meta.hardware_version = hardware_version; + meta.watchdog = platform::watchdog_detected(); + + if let Some(panic_data) = panic_persist::get_panic_message_utf8() { + meta.panic_info = panic_data; + } + + if let Some(git_revision) = build_info::GIT_COMMIT_HASH { + meta.git_revision = git_revision; + } + + if let Some(dirty) = build_info::GIT_DIRTY { + meta.git_dirty = dirty; + } + + if let Some(version) = build_info::GIT_VERSION { + meta.firmware_version = version; + } + + platform::clear_reset_flags(); + + meta + } +} diff --git a/src/hardware/mod.rs b/src/hardware/mod.rs index 14f35369..1ccafb72 100644 --- a/src/hardware/mod.rs +++ b/src/hardware/mod.rs @@ -5,6 +5,7 @@ //! Unauthorized usage, editing, or copying is strictly prohibited. //! Proprietary and confidential. +use core::fmt::Write; use enum_iterator::IntoEnumIterator; use serde::{Deserialize, Serialize}; use stm32f4xx_hal as hal; @@ -13,6 +14,7 @@ pub mod booster_channels; pub mod chassis_fans; pub mod delay; pub mod external_mac; +pub mod metadata; mod mutex; pub mod net_interface; pub mod platform; @@ -115,3 +117,11 @@ impl core::fmt::Display for HardwareVersion { } } } + +impl serde::Serialize for HardwareVersion { + fn serialize(&self, serializer: S) -> Result { + let mut version_string: heapless::String<32> = heapless::String::new(); + write!(&mut version_string, "{}", self).unwrap(); + serializer.serialize_str(&version_string) + } +} diff --git a/src/hardware/setup.rs b/src/hardware/setup.rs index ce8c0b1a..e8acafe6 100644 --- a/src/hardware/setup.rs +++ b/src/hardware/setup.rs @@ -9,7 +9,9 @@ use super::{ booster_channels::BoosterChannels, chassis_fans::ChassisFans, delay::AsmDelay, - external_mac, net_interface, platform, + external_mac, + metadata::ApplicationMetadata, + net_interface, platform, rf_channel::{AdcPin, ChannelPins as RfChannelPins}, user_interface::{UserButtons, UserLeds}, HardwareVersion, NetworkManager, NetworkStack, SystemTimer, Systick, UsbBus, CPU_FREQ, I2C, @@ -84,7 +86,7 @@ pub struct BoosterDevices { pub usb_device: UsbDevice<'static, UsbBus>, pub usb_serial: usbd_serial::SerialPort<'static, UsbBus>, pub settings: BoosterSettings, - pub hardware_version: HardwareVersion, + pub metadata: &'static ApplicationMetadata, pub systick: Systick, } @@ -272,17 +274,21 @@ pub fn setup( BoosterSettings::new(eui) }; - // Read the hardware version pins. - let hardware_version = { - let hwrev0 = gpiof.pf0.into_pull_down_input(); - let hwrev1 = gpiof.pf1.into_pull_down_input(); - let hwrev2 = gpiof.pf2.into_pull_down_input(); + let metadata = { + // Read the hardware version pins. + let hardware_version = { + let hwrev0 = gpiof.pf0.into_pull_down_input(); + let hwrev1 = gpiof.pf1.into_pull_down_input(); + let hwrev2 = gpiof.pf2.into_pull_down_input(); + + HardwareVersion::from( + *0u8.set_bit(0, hwrev0.is_high().unwrap()) + .set_bit(1, hwrev1.is_high().unwrap()) + .set_bit(2, hwrev2.is_high().unwrap()), + ) + }; - HardwareVersion::from( - *0u8.set_bit(0, hwrev0.is_high().unwrap()) - .set_bit(1, hwrev1.is_high().unwrap()) - .set_bit(2, hwrev2.is_high().unwrap()), - ) + ApplicationMetadata::new(hardware_version) }; let (manager, network_stack) = { @@ -431,7 +437,7 @@ pub fn setup( usb_device, usb_serial, watchdog, - hardware_version, + metadata, systick, } } diff --git a/src/main.rs b/src/main.rs index 89d6e202..30a3580e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,6 +118,7 @@ mod app { booster.settings.id(), settings, clock, + booster.metadata, ), watchdog: watchdog_manager, }, @@ -128,7 +129,7 @@ mod app { booster.usb_device, booster.usb_serial, booster.settings, - booster.hardware_version, + booster.metadata, ), }, init::Monotonics(booster.systick), diff --git a/src/net/mod.rs b/src/net/mod.rs index e7c265a8..bf7ed631 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -42,6 +42,7 @@ impl NetworkDevices { identifier: &str, settings: crate::RuntimeSettings, clock: SystemTimer, + metadata: &'static crate::hardware::metadata::ApplicationMetadata, ) -> Self { let crate::hardware::setup::NetworkDevices { manager, @@ -82,6 +83,7 @@ impl NetworkDevices { shared.acquire_stack(), clock, identifier, + metadata, ), settings: miniconf::MqttClient::new( shared.acquire_stack(), diff --git a/src/net/mqtt_control.rs b/src/net/mqtt_control.rs index 305f3a30..e3420778 100644 --- a/src/net/mqtt_control.rs +++ b/src/net/mqtt_control.rs @@ -5,7 +5,7 @@ //! Unauthorized usage, editing, or copying is strictly prohibited. //! Proprietary and confidential. use crate::{ - hardware::{setup::MainBus, SystemTimer}, + hardware::{metadata::ApplicationMetadata, setup::MainBus, SystemTimer}, Channel, }; @@ -41,8 +41,10 @@ struct ChannelBiasResponse { /// Represents a means of handling MQTT-based control interface. pub struct TelemetryClient { mqtt: minimq::Minimq, - telemetry_prefix: String<128>, + prefix: String<128>, telemetry_period: u64, + meta_published: bool, + metadata: &'static ApplicationMetadata, } impl TelemetryClient { @@ -52,17 +54,34 @@ impl TelemetryClient { stack: super::NetworkStackProxy, clock: SystemTimer, id: &str, + metadata: &'static ApplicationMetadata, ) -> Self { let mut client_id: String<64> = String::new(); write!(&mut client_id, "booster-{}-tlm", id).unwrap(); - let mut telemetry_prefix: String<128> = String::new(); - write!(&mut telemetry_prefix, "dt/sinara/booster/{}/telemetry", id).unwrap(); + let mut prefix: String<128> = String::new(); + write!(&mut prefix, "dt/sinara/booster/{}", id).unwrap(); + + let mut mqtt = minimq::Minimq::new(broker, &client_id, stack, clock).unwrap(); + + let mut topic: String<64> = String::new(); + write!(&mut topic, "{}/alive/meta", prefix).unwrap(); + mqtt.client + .set_will( + &topic, + "".as_bytes(), + minimq::QoS::AtMostOnce, + minimq::Retain::NotRetained, + &[], + ) + .unwrap(); Self { - mqtt: minimq::Minimq::new(broker, &client_id, stack, clock).unwrap(), - telemetry_prefix, + mqtt, + prefix, telemetry_period: DEFAULT_TELEMETRY_PERIOD_SECS, + meta_published: false, + metadata, } } @@ -73,7 +92,7 @@ impl TelemetryClient { /// * `telemetry` - The associated telemetry of the channel to report. pub fn report_telemetry(&mut self, channel: Channel, telemetry: &impl Serialize) { let mut topic: String<64> = String::new(); - write!(&mut topic, "{}/ch{}", self.telemetry_prefix, channel as u8).unwrap(); + write!(&mut topic, "{}/telemetry/ch{}", self.prefix, channel as u8).unwrap(); let message: String<1024> = serde_json_core::to_string(telemetry).unwrap(); @@ -93,6 +112,30 @@ impl TelemetryClient { /// Handle the MQTT-based telemetry interface. pub fn update(&mut self) { self.mqtt.poll(|_, _, _, _| {}).ok(); + + if self.mqtt.client.is_connected() { + if !self.meta_published { + let mut topic: String<64> = String::new(); + write!(&mut topic, "{}/alive/meta", self.prefix).unwrap(); + let message: String<1024> = serde_json_core::to_string(&self.metadata).unwrap(); + if self + .mqtt + .client + .publish( + &topic, + &message.into_bytes(), + minimq::QoS::AtMostOnce, + minimq::Retain::Retained, + &[], + ) + .is_ok() + { + self.meta_published = true; + } + } + } else { + self.meta_published = false + } } /// Get the period between telemetry updates in CPU cycles. diff --git a/src/serial_terminal.rs b/src/serial_terminal.rs index 7841a661..a34f6b50 100644 --- a/src/serial_terminal.rs +++ b/src/serial_terminal.rs @@ -5,7 +5,7 @@ //! Unauthorized usage, editing, or copying is strictly prohibited. //! Proprietary and confidential. use super::{ - hardware::{platform, HardwareVersion, UsbBus}, + hardware::{platform, UsbBus}, BoosterSettings, }; use bbqueue::BBBuffer; @@ -16,10 +16,6 @@ use usbd_serial::UsbError; use core::{fmt::Write, str::FromStr}; use minimq::embedded_nal::Ipv4Addr; -mod build_info { - include!(concat!(env!("OUT_DIR"), "/built.rs")); -} - #[derive(Logos)] enum Token { #[error] @@ -104,7 +100,7 @@ pub struct SerialTerminal { input_buffer: Vec, output_buffer_producer: bbqueue::Producer<'static, 1024>, output_buffer_consumer: bbqueue::Consumer<'static, 1024>, - hardware_version: HardwareVersion, + metadata: &'static crate::hardware::metadata::ApplicationMetadata, } impl SerialTerminal { @@ -113,7 +109,7 @@ impl SerialTerminal { usb_device: usb_device::device::UsbDevice<'static, UsbBus>, usb_serial: usbd_serial::SerialPort<'static, UsbBus>, settings: BoosterSettings, - hardware_version: HardwareVersion, + metadata: &'static crate::hardware::metadata::ApplicationMetadata, ) -> Self { let (producer, consumer) = OUTPUT_BUFFER.try_split().unwrap(); Self { @@ -123,7 +119,7 @@ impl SerialTerminal { input_buffer: Vec::new(), output_buffer_producer: producer, output_buffer_consumer: consumer, - hardware_version, + metadata, } } @@ -154,9 +150,7 @@ impl SerialTerminal { writeln!( &mut msg, "{:<20}: {} [{}]", - "Version", - build_info::GIT_VERSION.unwrap_or("Unspecified"), - build_info::PROFILE + "Version", self.metadata.firmware_version, self.metadata.profile, ) .unwrap_or_else(|_| { msg = String::from("Version: too long"); @@ -167,7 +161,7 @@ impl SerialTerminal { writeln!( &mut msg, "{:<20}: {}", - "Hardware Revision", self.hardware_version + "Hardware Revision", self.metadata.hardware_version ) .unwrap_or_else(|_| { msg = String::from("Hardware version: too long"); @@ -178,8 +172,7 @@ impl SerialTerminal { writeln!( &mut msg, "{:<20}: {}", - "Build Time", - build_info::BUILT_TIME_UTC + "Build Time", self.metadata.build_time_utc, ) .unwrap_or_else(|_| { msg = String::from("Build: too long"); @@ -190,8 +183,7 @@ impl SerialTerminal { writeln!( &mut msg, "{:<20}: {}", - "Rustc Version", - build_info::RUSTC_VERSION + "Rustc Version", self.metadata.rust_version, ) .unwrap_or_else(|_| { msg = String::from("Rustc Version: too long"); @@ -202,9 +194,7 @@ impl SerialTerminal { writeln!( &mut msg, "{:<20}: {} (dirty = {})", - "Git revision", - build_info::GIT_COMMIT_HASH.unwrap_or("Unspecified"), - build_info::GIT_DIRTY.unwrap_or(false) + "Git revision", self.metadata.git_revision, self.metadata.git_dirty, ) .unwrap_or_else(|_| { msg = String::from("Git revision: too long"); @@ -212,7 +202,7 @@ impl SerialTerminal { self.write(msg.as_bytes()); msg.clear(); - writeln!(&mut msg, "{:<20}: {}", "Features", build_info::FEATURES_STR) + writeln!(&mut msg, "{:<20}: {}", "Features", self.metadata.features) .unwrap_or_else(|_| { msg = String::from("Features: too long"); }); @@ -223,7 +213,7 @@ impl SerialTerminal { // string. write!(&mut msg, "{:<20}: ", "Panic Info").unwrap(); self.write(msg.as_bytes()); - self.write(panic_persist::get_panic_message_bytes().unwrap_or(b"None")); + self.write(self.metadata.panic_info.as_bytes()); self.write("\n".as_bytes()); msg.clear(); @@ -232,15 +222,10 @@ impl SerialTerminal { writeln!( &mut msg, "{:<20}: {}", - "Watchdog Detected", - platform::watchdog_detected() + "Watchdog Detected", self.metadata.watchdog ) .unwrap(); self.write(msg.as_bytes()); - - // Reading the panic message above clears the panic message, so similarly, we - // should also clear the watchdog once read. - platform::clear_reset_flags(); } Request::WriteIpAddress(prop, addr) => match prop { From 9e3b19f1f34f9b3036bd032945f9b2210005981f Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Mon, 2 May 2022 12:42:36 +0200 Subject: [PATCH 2/6] Refactoring reset flag management --- book/src/overview.md | 6 ++++++ src/hardware/metadata.rs | 2 -- src/serial_terminal.rs | 4 ++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/book/src/overview.md b/book/src/overview.md index b772c8cb..d6844651 100644 --- a/book/src/overview.md +++ b/book/src/overview.md @@ -26,3 +26,9 @@ this manual: 1. Pressing any of two the front-panel buttons 1. Communicating with Booster over the USB port 1. Communicating with Booster over ethernet via MQTT + +# Fault Mode + +When Booster encounters a software fault, it goes into a safe mode where no channels will be +enabled. To acknowledge and clear the fault, utilize the `service` command from the front panel USB +port. diff --git a/src/hardware/metadata.rs b/src/hardware/metadata.rs index 1bcbdd18..d4d5dac2 100644 --- a/src/hardware/metadata.rs +++ b/src/hardware/metadata.rs @@ -55,8 +55,6 @@ impl ApplicationMetadata { meta.firmware_version = version; } - platform::clear_reset_flags(); - meta } } diff --git a/src/serial_terminal.rs b/src/serial_terminal.rs index a34f6b50..27bc62a1 100644 --- a/src/serial_terminal.rs +++ b/src/serial_terminal.rs @@ -226,6 +226,10 @@ impl SerialTerminal { ) .unwrap(); self.write(msg.as_bytes()); + + // Use this as a mechanism for the user to "acknowledge" the service state of + // the device. This will allow RF channels to re-enable. + platform::clear_reset_flags(); } Request::WriteIpAddress(prop, addr) => match prop { From 860649b49128d57fe3fd17d9b5123c7de5e928fd Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Mon, 2 May 2022 12:50:46 +0200 Subject: [PATCH 3/6] Fixing retention settings --- Cargo.lock | 12 ++++++------ src/net/mqtt_control.rs | 19 ++----------------- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e4ed681d..11a5693c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -201,9 +201,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.72" +version = "1.0.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" dependencies = [ "jobserver", ] @@ -636,9 +636,9 @@ dependencies = [ [[package]] name = "libz-sys" -version = "1.1.3" +version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de5435b8549c16d423ed0c03dbaafe57cf6c3344744f1242520d59c9d8ecec66" +checksum = "6f35facd4a5673cb5a48822be2be1d4236c1c99cb4113cab7061ac720d5bf859" dependencies = [ "cc", "libc", @@ -911,9 +911,9 @@ checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" [[package]] name = "pkg-config" -version = "0.3.24" +version = "0.3.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58893f751c9b0412871a09abd62ecd2a00298c6c83befa223ef98c52aef40cbe" +checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" [[package]] name = "postcard" diff --git a/src/net/mqtt_control.rs b/src/net/mqtt_control.rs index e3420778..05d501ec 100644 --- a/src/net/mqtt_control.rs +++ b/src/net/mqtt_control.rs @@ -61,23 +61,8 @@ impl TelemetryClient { let mut prefix: String<128> = String::new(); write!(&mut prefix, "dt/sinara/booster/{}", id).unwrap(); - - let mut mqtt = minimq::Minimq::new(broker, &client_id, stack, clock).unwrap(); - - let mut topic: String<64> = String::new(); - write!(&mut topic, "{}/alive/meta", prefix).unwrap(); - mqtt.client - .set_will( - &topic, - "".as_bytes(), - minimq::QoS::AtMostOnce, - minimq::Retain::NotRetained, - &[], - ) - .unwrap(); - Self { - mqtt, + mqtt: minimq::Minimq::new(broker, &client_id, stack, clock).unwrap(), prefix, telemetry_period: DEFAULT_TELEMETRY_PERIOD_SECS, meta_published: false, @@ -125,7 +110,7 @@ impl TelemetryClient { &topic, &message.into_bytes(), minimq::QoS::AtMostOnce, - minimq::Retain::Retained, + minimq::Retain::NotRetained, &[], ) .is_ok() From d4e11981f226b89103219ffa82b50d547144773d Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Mon, 2 May 2022 12:58:27 +0200 Subject: [PATCH 4/6] Fixing safety concerns --- src/hardware/metadata.rs | 45 ++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/hardware/metadata.rs b/src/hardware/metadata.rs index d4d5dac2..37a438a5 100644 --- a/src/hardware/metadata.rs +++ b/src/hardware/metadata.rs @@ -1,3 +1,9 @@ +//! Booster run-time application metadata +//! +//! # Copyright +//! Copyright (C) 2020 QUARTIQ GmbH - All Rights Reserved +//! Unauthorized usage, editing, or copying is strictly prohibited. +//! Proprietary and confidential. use serde::Serialize; use super::{platform, HardwareVersion}; @@ -6,19 +12,6 @@ mod build_info { include!(concat!(env!("OUT_DIR"), "/built.rs")); } -static mut METADATA: ApplicationMetadata = ApplicationMetadata { - firmware_version: "Unspecified", - build_time_utc: build_info::BUILT_TIME_UTC, - rust_version: build_info::RUSTC_VERSION, - profile: build_info::PROFILE, - git_revision: "Unspecified", - git_dirty: true, - features: build_info::FEATURES_STR, - panic_info: "None", - watchdog: false, - hardware_version: HardwareVersion::Unknown(0), -}; - #[derive(Serialize)] pub struct ApplicationMetadata { pub firmware_version: &'static str, @@ -34,10 +27,30 @@ pub struct ApplicationMetadata { } impl ApplicationMetadata { + /// Construct the global metadata. + /// + /// # Note + /// This may only be called once. + /// + /// # Args + /// * `hardware_version` - The hardware version detected. + /// + /// # Returns + /// A reference to the global metadata. pub fn new(hardware_version: HardwareVersion) -> &'static ApplicationMetadata { - let mut meta = unsafe { &mut METADATA }; - meta.hardware_version = hardware_version; - meta.watchdog = platform::watchdog_detected(); + let mut meta = cortex_m::singleton!(: ApplicationMetadata = ApplicationMetadata { + firmware_version: "Unspecified", + build_time_utc: build_info::BUILT_TIME_UTC, + rust_version: build_info::RUSTC_VERSION, + profile: build_info::PROFILE, + git_revision: "Unspecified", + git_dirty: true, + features: build_info::FEATURES_STR, + panic_info: "None", + watchdog: platform::watchdog_detected(), + hardware_version, + }) + .unwrap(); if let Some(panic_data) = panic_persist::get_panic_message_utf8() { meta.panic_info = panic_data; From 9b1f78d48499c1ccd2b48653c7f636aba48ba416 Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Tue, 3 May 2022 16:16:42 +0200 Subject: [PATCH 5/6] Removing Built time and git hash --- Cargo.lock | 43 ----------------------------------- Cargo.toml | 2 +- src/hardware/metadata.rs | 8 ------- src/net/mqtt_control.rs | 48 ++++++++++++++++++++++++++++------------ src/serial_terminal.rs | 22 ------------------ 5 files changed, 35 insertions(+), 88 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 11a5693c..473529b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -168,7 +168,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4f346b6890a0dfa7266974910e7df2d5088120dd54721b9b0e5aae1ae5e05715" dependencies = [ "cargo-lock", - "chrono", "git2", ] @@ -226,11 +225,8 @@ version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" dependencies = [ - "libc", "num-integer", "num-traits", - "time", - "winapi", ] [[package]] @@ -1334,17 +1330,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "time" -version = "0.1.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" -dependencies = [ - "libc", - "wasi", - "winapi", -] - [[package]] name = "tinyvec" version = "1.5.1" @@ -1481,31 +1466,3 @@ dependencies = [ "embedded-nal", "nb 1.0.0", ] - -[[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index bb9fa0ad..fa751f0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,7 +40,7 @@ minireq = "0.1" rtt-target = {version = "0.3", features=["cortex-m"]} [build-dependencies] -built = { version = "0.5", features = ["git2", "chrono"], default-features = false } +built = { version = "0.5", features = ["git2"], default-features = false } # Add RawDevice support for the W5500 to operate as an external MAC [patch.crates-io.w5500] diff --git a/src/hardware/metadata.rs b/src/hardware/metadata.rs index 37a438a5..42371ab3 100644 --- a/src/hardware/metadata.rs +++ b/src/hardware/metadata.rs @@ -15,9 +15,7 @@ mod build_info { #[derive(Serialize)] pub struct ApplicationMetadata { pub firmware_version: &'static str, - pub build_time_utc: &'static str, pub rust_version: &'static str, - pub git_revision: &'static str, pub profile: &'static str, pub git_dirty: bool, pub features: &'static str, @@ -40,10 +38,8 @@ impl ApplicationMetadata { pub fn new(hardware_version: HardwareVersion) -> &'static ApplicationMetadata { let mut meta = cortex_m::singleton!(: ApplicationMetadata = ApplicationMetadata { firmware_version: "Unspecified", - build_time_utc: build_info::BUILT_TIME_UTC, rust_version: build_info::RUSTC_VERSION, profile: build_info::PROFILE, - git_revision: "Unspecified", git_dirty: true, features: build_info::FEATURES_STR, panic_info: "None", @@ -56,10 +52,6 @@ impl ApplicationMetadata { meta.panic_info = panic_data; } - if let Some(git_revision) = build_info::GIT_COMMIT_HASH { - meta.git_revision = git_revision; - } - if let Some(dirty) = build_info::GIT_DIRTY { meta.git_dirty = dirty; } diff --git a/src/net/mqtt_control.rs b/src/net/mqtt_control.rs index 05d501ec..22b61649 100644 --- a/src/net/mqtt_control.rs +++ b/src/net/mqtt_control.rs @@ -17,6 +17,9 @@ use core::fmt::Write; use heapless::String; use serde::Serialize; +/// Default metadata message if formatting errors occur. +const DEFAULT_METADATA: &'static str = "{\"message\":\"Truncated: See USB terminal\"}"; + type MinireqResponse = Result< minireq::Response<256>, minireq::Error<::Error>, @@ -98,28 +101,45 @@ impl TelemetryClient { pub fn update(&mut self) { self.mqtt.poll(|_, _, _, _| {}).ok(); - if self.mqtt.client.is_connected() { - if !self.meta_published { - let mut topic: String<64> = String::new(); - write!(&mut topic, "{}/alive/meta", self.prefix).unwrap(); - let message: String<1024> = serde_json_core::to_string(&self.metadata).unwrap(); - if self - .mqtt + if !self.mqtt.client.is_connected() { + self.meta_published = false; + return; + } + + // If the metadata has not yet been published, but we can publish it, do so now. + if !self.meta_published && self.mqtt.client.can_publish(minimq::QoS::AtMostOnce) { + let mut topic: String<64> = String::new(); + write!(&mut topic, "{}/alive/meta", self.prefix).unwrap(); + let message: String<512> = serde_json_core::to_string(&self.metadata) + .unwrap_or_else(|_| String::from(DEFAULT_METADATA)); + + if self + .mqtt + .client + .publish( + &topic, + &message.into_bytes(), + minimq::QoS::AtMostOnce, + minimq::Retain::NotRetained, + &[], + ) + .is_err() + { + // Note(unwrap): We can guarantee that this message will be sent because we checked + // for ability to publish above. + self.mqtt .client .publish( &topic, - &message.into_bytes(), + DEFAULT_METADATA.as_bytes(), minimq::QoS::AtMostOnce, minimq::Retain::NotRetained, &[], ) - .is_ok() - { - self.meta_published = true; - } + .unwrap(); } - } else { - self.meta_published = false + + self.meta_published = true; } } diff --git a/src/serial_terminal.rs b/src/serial_terminal.rs index 27bc62a1..bb899b31 100644 --- a/src/serial_terminal.rs +++ b/src/serial_terminal.rs @@ -168,17 +168,6 @@ impl SerialTerminal { }); self.write(msg.as_bytes()); - msg.clear(); - writeln!( - &mut msg, - "{:<20}: {}", - "Build Time", self.metadata.build_time_utc, - ) - .unwrap_or_else(|_| { - msg = String::from("Build: too long"); - }); - self.write(msg.as_bytes()); - msg.clear(); writeln!( &mut msg, @@ -190,17 +179,6 @@ impl SerialTerminal { }); self.write(msg.as_bytes()); - msg.clear(); - writeln!( - &mut msg, - "{:<20}: {} (dirty = {})", - "Git revision", self.metadata.git_revision, self.metadata.git_dirty, - ) - .unwrap_or_else(|_| { - msg = String::from("Git revision: too long"); - }); - self.write(msg.as_bytes()); - msg.clear(); writeln!(&mut msg, "{:<20}: {}", "Features", self.metadata.features) .unwrap_or_else(|_| { From ecb5e8a3638984edc4882b0d4976d29de80d190c Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Tue, 3 May 2022 16:34:05 +0200 Subject: [PATCH 6/6] Fixing clippy --- src/net/mqtt_control.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/net/mqtt_control.rs b/src/net/mqtt_control.rs index 22b61649..fbb5aff5 100644 --- a/src/net/mqtt_control.rs +++ b/src/net/mqtt_control.rs @@ -18,7 +18,7 @@ use heapless::String; use serde::Serialize; /// Default metadata message if formatting errors occur. -const DEFAULT_METADATA: &'static str = "{\"message\":\"Truncated: See USB terminal\"}"; +const DEFAULT_METADATA: &str = "{\"message\":\"Truncated: See USB terminal\"}"; type MinireqResponse = Result< minireq::Response<256>,