From 033420b9346e376cc7ec1ccdbc8ee061f3309cbe Mon Sep 17 00:00:00 2001 From: Ryan Summers Date: Tue, 13 Apr 2021 15:38:30 +0200 Subject: [PATCH] Adding custom identifier prefix calculations --- src/bin/dual-iir.rs | 5 +++- src/bin/lockin-external.rs | 7 +++++- src/hardware/configuration.rs | 4 +++- src/hardware/design_parameters.rs | 2 +- src/net/mod.rs | 40 +++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/bin/dual-iir.rs b/src/bin/dual-iir.rs index 0e2215b..b0072af 100644 --- a/src/bin/dual-iir.rs +++ b/src/bin/dual-iir.rs @@ -59,7 +59,10 @@ const APP: () = { let mqtt_settings = MqttSettings::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 b62ea42..34a3d5b 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_settings = MqttSettings::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 82158f3..fdde1f4 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. @@ -544,7 +545,7 @@ pub fn setup( smoltcp::iface::NeighborCache::new(&mut store.neighbor_cache[..]); let interface = smoltcp::iface::EthernetInterfaceBuilder::new(eth_dma) - .ethernet_addr(mac_addr) + .ethernet_addr(mac_addr.clone()) .neighbor_cache(neighbor_cache) .ip_addrs(&mut store.ip_addrs[..]) .routes(routes) @@ -599,6 +600,7 @@ pub fn setup( Some(dhcp_client), ), phy: lan8742a, + mac_address: mac_addr, } }; diff --git a/src/hardware/design_parameters.rs b/src/hardware/design_parameters.rs index 9a4279b..0ae4c35 100644 --- a/src/hardware/design_parameters.rs +++ b/src/hardware/design_parameters.rs @@ -51,4 +51,4 @@ pub const SAMPLE_BUFFER_SIZE_LOG2: u8 = 3; pub const SAMPLE_BUFFER_SIZE: usize = 1 << SAMPLE_BUFFER_SIZE_LOG2; // 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]; diff --git a/src/net/mod.rs b/src/net/mod.rs index f38f3d9..3ad21b6 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -2,6 +2,10 @@ use crate::hardware::{ design_parameters::MQTT_BROKER, CycleCounter, EthernetPhy, NetworkStack, }; +use core::fmt::Write; + +use heapless::{consts, String}; + use miniconf::{minimq, MqttInterface}; /// 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 { + 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 +}