diff --git a/CHANGELOG.md b/CHANGELOG.md index 512261a7..db72a63e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed * Network stack is randomly seeded on startup so that random ports are used. * Serial terminal replaced with `menu` for simplicity +* The broker can now be specified using DNS ## [0.5.0] - 03-07-2023 diff --git a/src/main.rs b/src/main.rs index 2286d81c..3ae4b81e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,10 +96,9 @@ mod app { SharedResources { main_bus: booster.main_bus, net_devices: net::NetworkDevices::new( - // TODO: Replace with hostname-based broker. - booster.settings.properties.broker(), + &booster.settings.properties.broker, booster.network_stack, - &booster.settings.properties.id.0, + &booster.settings.properties.id, settings, clock, booster.metadata, diff --git a/src/net/mod.rs b/src/net/mod.rs index f4a5d1fc..b172cfae 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -39,7 +39,7 @@ pub struct NetworkDevices { crate::RuntimeSettings, NetworkStackProxy, SystemTimer, - minireq::minimq::broker::IpBroker, + minireq::minimq::broker::NamedBroker, 4, >, pub control: minireq::Minireq< @@ -47,7 +47,7 @@ pub struct NetworkDevices { MainBus, NetworkStackProxy, SystemTimer, - minireq::minimq::broker::IpBroker, + minireq::minimq::broker::NamedBroker, mqtt_control::Error, >, stack: NetworkStackProxy, @@ -61,7 +61,7 @@ impl NetworkDevices { /// * `stack` - The network stack to use for communications. /// * `identifier` - The unique identifier of this device. pub fn new( - broker: minimq::embedded_nal::IpAddr, + broker: &str, stack: NetworkStack, identifier: &str, settings: crate::RuntimeSettings, @@ -81,7 +81,8 @@ impl NetworkDevices { let mut client_id: String<128> = String::new(); write!(&mut client_id, "booster-{}-req", identifier).unwrap(); - let broker = minireq::minimq::broker::IpBroker::new(broker); + let broker = + minireq::minimq::broker::NamedBroker::new(broker, shared.acquire_stack()).unwrap(); let config = minireq::minimq::ConfigBuilder::new(broker, &mut store.settings) .client_id(&client_id) .unwrap(); @@ -104,7 +105,8 @@ impl NetworkDevices { let mut client_id: String<64> = String::new(); write!(&mut client_id, "booster-{}-tlm", identifier).unwrap(); - let broker = minireq::minimq::broker::IpBroker::new(broker); + let broker = + minireq::minimq::broker::NamedBroker::new(broker, shared.acquire_stack()).unwrap(); let config = miniconf::minimq::ConfigBuilder::new(broker, &mut store.telemetry) // The telemetry client doesn't do much in terms of receiving data, so reserve the // buffer for transmission. @@ -122,7 +124,8 @@ impl NetworkDevices { let mut client_id: String<128> = String::new(); write!(&mut client_id, "booster-{}-settings", identifier).unwrap(); - let broker = minireq::minimq::broker::IpBroker::new(broker); + let broker = + minireq::minimq::broker::NamedBroker::new(broker, shared.acquire_stack()).unwrap(); let config = miniconf::minimq::ConfigBuilder::new(broker, &mut store.control) .client_id(&client_id) .unwrap(); diff --git a/src/net/mqtt_control.rs b/src/net/mqtt_control.rs index 5d31515b..ea9aab56 100644 --- a/src/net/mqtt_control.rs +++ b/src/net/mqtt_control.rs @@ -74,7 +74,12 @@ struct ChannelBiasResponse { /// Represents a means of handling MQTT-based control interface. pub struct TelemetryClient { - mqtt: minimq::Minimq<'static, NetworkStackProxy, SystemTimer, minimq::broker::IpBroker>, + mqtt: minimq::Minimq< + 'static, + NetworkStackProxy, + SystemTimer, + minimq::broker::NamedBroker, + >, prefix: String<128>, telemetry_period: u64, meta_published: bool, @@ -84,7 +89,12 @@ pub struct TelemetryClient { impl TelemetryClient { /// Construct the MQTT control manager. pub fn new( - mqtt: minimq::Minimq<'static, NetworkStackProxy, SystemTimer, minimq::broker::IpBroker>, + mqtt: minimq::Minimq< + 'static, + NetworkStackProxy, + SystemTimer, + minimq::broker::NamedBroker, + >, metadata: &'static ApplicationMetadata, prefix: &str, ) -> Self { diff --git a/src/settings/global_settings.rs b/src/settings/global_settings.rs index fe6a950a..604f51cb 100644 --- a/src/settings/global_settings.rs +++ b/src/settings/global_settings.rs @@ -1,6 +1,7 @@ //! Booster NGFW NVM settings use crate::{hardware::Eeprom, Error}; +use core::str::FromStr; use encdec::{Decode, DecodeOwned, Encode}; use heapless::String; use smoltcp_nal::smoltcp; @@ -137,10 +138,13 @@ impl From for SerializedMainBoardData { Self { version: d.version, ip: d.ip, - broker: d.broker, + broker: d + .broker + .parse() + .unwrap_or_else(|_| IpAddr::new(&[10, 0, 0, 2])), gateway: d.gateway, netmask: d.netmask, - id: d.id, + id: MqttIdentifier(d.id), fan_speed: d.fan_speed, } } @@ -148,14 +152,16 @@ impl From for SerializedMainBoardData { impl SerializedMainBoardData { fn with_mac(self, eui48: &[u8; 6]) -> BoosterMainBoardData { + let mut broker = String::new(); + write!(&mut broker, "{}", self.broker.0).unwrap(); BoosterMainBoardData { mac: smoltcp_nal::smoltcp::wire::EthernetAddress(*eui48), version: self.version, ip: self.ip, - broker: self.broker, + broker, gateway: self.gateway, netmask: self.netmask, - id: self.id, + id: self.id.0, fan_speed: self.fan_speed, } } @@ -172,10 +178,10 @@ pub struct BoosterMainBoardData { pub mac: smoltcp_nal::smoltcp::wire::EthernetAddress, pub ip: IpAddr, - pub broker: IpAddr, + pub broker: heapless::String<255>, pub gateway: IpAddr, pub netmask: IpAddr, - pub id: MqttIdentifier, + pub id: heapless::String<23>, pub fan_speed: f32, } @@ -206,10 +212,10 @@ impl BoosterMainBoardData { mac: smoltcp_nal::smoltcp::wire::EthernetAddress(*eui48), version: EXPECTED_VERSION, ip: IpAddr::new(&[0, 0, 0, 0]), - broker: IpAddr::new(&[10, 0, 0, 2]), + broker: String::from_str("10.0.0.2").unwrap(), gateway: IpAddr::new(&[0, 0, 0, 0]), netmask: IpAddr::new(&[0, 0, 0, 0]), - id: MqttIdentifier(name), + id: name, fan_speed: DEFAULT_FAN_SPEED, } } @@ -263,7 +269,7 @@ impl BoosterMainBoardData { } pub fn validate(&self) -> bool { - if !identifier_is_valid(&self.id.0) { + if !identifier_is_valid(&self.id) { log::error!("The ID must be 23 or less alpha-numeric characters (or '-')"); return false; } @@ -299,13 +305,6 @@ impl BoosterMainBoardData { smoltcp::wire::IpCidr::new(smoltcp::wire::IpAddress::Ipv4(ip_addr), prefix) } - - pub fn broker(&self) -> minimq::embedded_nal::IpAddr { - let octets = self.broker.0 .0; - minimq::embedded_nal::IpAddr::V4(minimq::embedded_nal::Ipv4Addr::new( - octets[0], octets[1], octets[2], octets[3], - )) - } } /// Booster device-wide configurable settings.