Adding custom identifier prefix calculations

This commit is contained in:
Ryan Summers 2021-04-13 15:38:30 +02:00
parent f96f49befa
commit 033420b934
5 changed files with 54 additions and 4 deletions

View File

@ -59,7 +59,10 @@ const APP: () = {
let mqtt_settings = MqttSettings::new( let mqtt_settings = MqttSettings::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_settings = MqttSettings::new( let mqtt_settings = MqttSettings::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.
@ -544,7 +545,7 @@ pub fn setup(
smoltcp::iface::NeighborCache::new(&mut store.neighbor_cache[..]); smoltcp::iface::NeighborCache::new(&mut store.neighbor_cache[..]);
let interface = smoltcp::iface::EthernetInterfaceBuilder::new(eth_dma) let interface = smoltcp::iface::EthernetInterfaceBuilder::new(eth_dma)
.ethernet_addr(mac_addr) .ethernet_addr(mac_addr.clone())
.neighbor_cache(neighbor_cache) .neighbor_cache(neighbor_cache)
.ip_addrs(&mut store.ip_addrs[..]) .ip_addrs(&mut store.ip_addrs[..])
.routes(routes) .routes(routes)
@ -599,6 +600,7 @@ pub fn setup(
Some(dhcp_client), Some(dhcp_client),
), ),
phy: lan8742a, phy: lan8742a,
mac_address: mac_addr,
} }
}; };

View File

@ -51,4 +51,4 @@ pub const SAMPLE_BUFFER_SIZE_LOG2: u8 = 3;
pub const SAMPLE_BUFFER_SIZE: usize = 1 << SAMPLE_BUFFER_SIZE_LOG2; pub const SAMPLE_BUFFER_SIZE: usize = 1 << SAMPLE_BUFFER_SIZE_LOG2;
// The MQTT broker IPv4 address // The MQTT broker IPv4 address
pub const MQTT_BROKER: [u8; 4] = [10, 34, 16, 10]; pub const MQTT_BROKER: [u8; 4] = [10, 35, 16, 10];

View File

@ -2,6 +2,10 @@ 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, MqttInterface}; use miniconf::{minimq, MqttInterface};
/// Potential actions for firmware to take. /// Potential actions for firmware to take.
@ -106,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
}