2021-04-29 17:54:16 +08:00
|
|
|
///! Stabilizer network management module
|
|
|
|
///!
|
|
|
|
///! # Design
|
|
|
|
///! The stabilizer network architecture supports numerous layers to permit transmission of
|
|
|
|
///! telemetry (via MQTT), configuration of run-time settings (via MQTT + Miniconf), and live data
|
|
|
|
///! streaming over raw UDP/TCP sockets. This module encompasses the main processing routines
|
|
|
|
///! related to Stabilizer networking operations.
|
2021-04-29 03:03:38 +08:00
|
|
|
use heapless::{consts, String};
|
2021-03-18 03:16:13 +08:00
|
|
|
|
2021-04-29 03:03:38 +08:00
|
|
|
use core::fmt::Write;
|
2021-04-13 21:38:30 +08:00
|
|
|
|
2021-04-29 17:54:16 +08:00
|
|
|
mod messages;
|
2021-04-29 03:03:38 +08:00
|
|
|
mod mqtt_interface;
|
2021-05-04 19:13:44 +08:00
|
|
|
use messages::{MqttMessage, SettingsResponse};
|
2021-04-29 03:03:38 +08:00
|
|
|
pub use mqtt_interface::MqttInterface;
|
2021-03-18 03:16:13 +08:00
|
|
|
|
2021-03-24 20:48:34 +08:00
|
|
|
/// Potential actions for firmware to take.
|
2021-03-18 03:16:13 +08:00
|
|
|
pub enum Action {
|
2021-03-24 20:48:34 +08:00
|
|
|
/// Indicates that firmware can sleep for the next event.
|
2021-03-18 03:16:13 +08:00
|
|
|
Sleep,
|
2021-03-24 20:48:34 +08:00
|
|
|
|
|
|
|
/// Indicates that settings have updated and firmware needs to propogate changes.
|
2021-03-18 03:16:13 +08:00
|
|
|
UpdateSettings,
|
|
|
|
}
|
|
|
|
|
2021-04-13 21:38:30 +08:00
|
|
|
/// 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
|
|
|
|
}
|