diff --git a/hitl/run.sh b/hitl/run.sh index 41d59a0..2af4732 100755 --- a/hitl/run.sh +++ b/hitl/run.sh @@ -27,6 +27,6 @@ sleep 30 ping -c 5 -w 20 stabilizer-hitl # Test the MQTT interface. -python3 miniconf.py dt/sinara/stabilizer afe/0='"G2"' -python3 miniconf.py dt/sinara/stabilizer afe/0='"G1"' iir_ch/0/0=\ +python3 miniconf.py dt/sinara/dual-iir/04-91-62-d9-7e-5f afe/0='"G2"' +python3 miniconf.py dt/sinara/dual-iir/04-91-62-d9-7e-5f afe/0='"G1"' iir_ch/0/0=\ '{"y_min": -32767, "y_max": 32767, "y_offset": 0, "ba": [1.0, 0, 0, 0, 0]}' diff --git a/src/bin/dual-iir.rs b/src/bin/dual-iir.rs index 8f07cf1..eaa82ae 100644 --- a/src/bin/dual-iir.rs +++ b/src/bin/dual-iir.rs @@ -62,7 +62,10 @@ const APP: () = { let mqtt_config = MiniconfInterface::new( stabilizer.net.stack, "", - "dt/sinara/stabilizer", + &net::get_device_prefix( + env!("CARGO_BIN_NAME"), + stabilizer.net.mac_address, + ), stabilizer.net.phy, stabilizer.cycle_counter, ); diff --git a/src/bin/lockin-external.rs b/src/bin/lockin-external.rs index 36bd882..082b0d2 100644 --- a/src/bin/lockin-external.rs +++ b/src/bin/lockin-external.rs @@ -8,6 +8,8 @@ use serde::Deserialize; use dsp::{Accu, Complex, ComplexExt, Lockin, RPLL}; +use stabilizer::net; + use stabilizer::hardware::{ design_parameters, setup, Adc0Input, Adc1Input, AfeGain, Dac0Output, Dac1Output, InputStamper, AFE0, AFE1, @@ -74,7 +76,10 @@ const APP: () = { let mqtt_config = MiniconfInterface::new( stabilizer.net.stack, "", - "dt/sinara/lockin", + &net::get_device_prefix( + env!("CARGO_BIN_NAME"), + stabilizer.net.mac_address, + ), stabilizer.net.phy, stabilizer.cycle_counter, ); diff --git a/src/hardware/configuration.rs b/src/hardware/configuration.rs index 5bc5a20..f453c11 100644 --- a/src/hardware/configuration.rs +++ b/src/hardware/configuration.rs @@ -57,6 +57,7 @@ impl NetStorage { pub struct NetworkDevices { pub stack: NetworkStack, pub phy: EthernetPhy, + pub mac_address: smoltcp::wire::EthernetAddress, } /// The available hardware interfaces on Stabilizer. @@ -618,6 +619,7 @@ pub fn setup( NetworkDevices { stack, phy: lan8742a, + mac_address: mac_addr, } }; diff --git a/src/net/mod.rs b/src/net/mod.rs index 23d712d..13b514c 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -2,6 +2,9 @@ use crate::hardware::{ design_parameters::MQTT_BROKER, CycleCounter, EthernetPhy, NetworkStack, }; +use core::fmt::Write; + +use heapless::{consts, String}; use miniconf::minimq; /// Potential actions for firmware to take. @@ -107,3 +110,39 @@ where } } } + +/// Get the MQTT prefix of a device. +/// +/// # Args +/// * `app` - The name of the application that is executing. +/// * `mac` - The ethernet MAC address of the device. +/// +/// # Returns +/// The MQTT prefix used for this device. +pub fn get_device_prefix( + app: &str, + mac: smoltcp_nal::smoltcp::wire::EthernetAddress, +) -> String { + let mac_string = { + let mut mac_string: String = String::new(); + let mac = mac.as_bytes(); + + // Note(unwrap): 32-bytes is guaranteed to be valid for any mac address, as the address has + // a fixed length. + write!( + &mut mac_string, + "{:02x}-{:02x}-{:02x}-{:02x}-{:02x}-{:02x}", + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] + ) + .unwrap(); + + mac_string + }; + + // Note(unwrap): The mac address + binary name must be short enough to fit into this string. If + // they are defined too long, this will panic and the device will fail to boot. + let mut prefix: String = String::new(); + write!(&mut prefix, "dt/sinara/{}/{}", app, mac_string).unwrap(); + + prefix +}