platform/
mqtt_app.rs

1use core::fmt::Write;
2use heapless::String;
3use miniconf::Tree;
4use smoltcp_nal::smoltcp::wire::EthernetAddress;
5
6/// Settings that are used for configuring the network interface to Stabilizer.
7#[derive(Clone, Debug, Tree)]
8#[tree(meta(doc, typename))]
9pub struct NetSettings {
10    /// The broker domain name (or IP address) to use for MQTT connections.
11    pub broker: String<255>,
12
13    /// The MQTT ID to use upon connection with a broker.
14    pub id: String<23>,
15
16    /// An optional static IP address to use. An unspecified IP address (or malformed address) will
17    /// use DHCP.
18    pub ip: String<15>,
19
20    #[tree(skip)]
21    /// The MAC address of Stabilizer, which is used to reinitialize the ID to default settings.
22    pub mac: EthernetAddress,
23}
24
25impl Default for NetSettings {
26    fn default() -> Self {
27        Self {
28            broker: String::try_from("mqtt").unwrap(),
29            ip: String::try_from("0.0.0.0").unwrap(),
30            id: String::try_from("<mac>").unwrap(),
31            mac: EthernetAddress::default(),
32        }
33    }
34}
35
36impl NetSettings {
37    pub fn new(mac: EthernetAddress) -> Self {
38        let mut id = String::new();
39        write!(&mut id, "{mac}").unwrap();
40
41        Self {
42            id,
43            mac,
44            ..Default::default()
45        }
46    }
47}
48
49pub trait AppSettings {
50    /// Construct the settings given known network settings.
51    fn new(net: NetSettings) -> Self;
52
53    /// Get the network settings from the application settings.
54    fn net(&self) -> &NetSettings;
55}