Merge pull request #337 from quartiq/rs/issue-276/unique-identifiers

Utilize unique MQTT prefix identifiers
This commit is contained in:
Robert Jördens 2021-04-20 13:46:41 +02:00 committed by GitHub
commit bba86f4b17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 4 deletions

View File

@ -27,6 +27,6 @@ sleep 30
ping -c 5 -w 20 stabilizer-hitl ping -c 5 -w 20 stabilizer-hitl
# Test the MQTT interface. # Test the MQTT interface.
python3 miniconf.py dt/sinara/stabilizer afe/0='"G2"' python3 miniconf.py dt/sinara/dual-iir/04-91-62-d9-7e-5f 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='"G1"' iir_ch/0/0=\
'{"y_min": -32767, "y_max": 32767, "y_offset": 0, "ba": [1.0, 0, 0, 0, 0]}' '{"y_min": -32767, "y_max": 32767, "y_offset": 0, "ba": [1.0, 0, 0, 0, 0]}'

View File

@ -62,7 +62,10 @@ const APP: () = {
let mqtt_config = MiniconfInterface::new( let mqtt_config = MiniconfInterface::new(
stabilizer.net.stack, stabilizer.net.stack,
"", "",
"dt/sinara/stabilizer", &net::get_device_prefix(
env!("CARGO_BIN_NAME"),
stabilizer.net.mac_address,
),
stabilizer.net.phy, stabilizer.net.phy,
stabilizer.cycle_counter, stabilizer.cycle_counter,
); );

View File

@ -8,6 +8,8 @@ use serde::Deserialize;
use dsp::{Accu, Complex, ComplexExt, Lockin, RPLL}; use dsp::{Accu, Complex, ComplexExt, Lockin, RPLL};
use stabilizer::net;
use stabilizer::hardware::{ use stabilizer::hardware::{
design_parameters, setup, Adc0Input, Adc1Input, AfeGain, Dac0Output, design_parameters, setup, Adc0Input, Adc1Input, AfeGain, Dac0Output,
Dac1Output, InputStamper, AFE0, AFE1, Dac1Output, InputStamper, AFE0, AFE1,
@ -74,7 +76,10 @@ const APP: () = {
let mqtt_config = MiniconfInterface::new( let mqtt_config = MiniconfInterface::new(
stabilizer.net.stack, stabilizer.net.stack,
"", "",
"dt/sinara/lockin", &net::get_device_prefix(
env!("CARGO_BIN_NAME"),
stabilizer.net.mac_address,
),
stabilizer.net.phy, stabilizer.net.phy,
stabilizer.cycle_counter, stabilizer.cycle_counter,
); );

View File

@ -57,6 +57,7 @@ impl NetStorage {
pub struct NetworkDevices { pub struct NetworkDevices {
pub stack: NetworkStack, pub stack: NetworkStack,
pub phy: EthernetPhy, pub phy: EthernetPhy,
pub mac_address: smoltcp::wire::EthernetAddress,
} }
/// The available hardware interfaces on Stabilizer. /// The available hardware interfaces on Stabilizer.
@ -618,6 +619,7 @@ pub fn setup(
NetworkDevices { NetworkDevices {
stack, stack,
phy: lan8742a, phy: lan8742a,
mac_address: mac_addr,
} }
}; };

View File

@ -2,6 +2,9 @@ use crate::hardware::{
design_parameters::MQTT_BROKER, CycleCounter, EthernetPhy, NetworkStack, design_parameters::MQTT_BROKER, CycleCounter, EthernetPhy, NetworkStack,
}; };
use core::fmt::Write;
use heapless::{consts, String};
use miniconf::minimq; use miniconf::minimq;
/// Potential actions for firmware to take. /// 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<consts::U128> {
let mac_string = {
let mut mac_string: String<consts::U32> = 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<consts::U128> = String::new();
write!(&mut prefix, "dt/sinara/{}/{}", app, mac_string).unwrap();
prefix
}