use std::fs; use anyhow::Context; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize, Clone)] pub struct FelinetConfig { pub enabled: bool, pub address: String, } impl Default for FelinetConfig { fn default() -> Self { FelinetConfig { enabled: false, address: "https://felinet.cats.radio".to_owned() } } } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct TunnelConfig { pub enabled: bool, pub local_ip: String, pub netmask: String, } impl Default for TunnelConfig { fn default() -> Self { TunnelConfig { enabled: false, local_ip: "10.73.14.1".to_owned(), netmask: "255.255.255.0".to_owned(), } } } pub(crate) type DurationSeconds = u32; #[derive(Debug, Serialize, Deserialize, Clone)] pub struct BeaconConfig { // A period of zero means beaconing is disabled pub period_seconds: DurationSeconds, #[serde(default)] pub max_hops: u8, pub latitude: Option, pub longitude: Option, pub altitude: Option, pub comment: Option, pub antenna_height: Option, pub antenna_gain: Option, pub tx_power: Option, // dBm } impl Default for BeaconConfig { fn default() -> Self { BeaconConfig { period_seconds: 0, max_hops: 3, latitude: None, longitude: None, altitude: None, comment: None, antenna_height: None, antenna_gain: None, tx_power: None, } } } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Config { pub freq: u32, // kHz pub callsign: String, pub ssid: u8, #[serde(default)] pub icon: u16, pub felinet: FelinetConfig, pub beacon: BeaconConfig, pub tunnel: TunnelConfig, } impl Default for Config { fn default() -> Self { Config { freq: 430500, callsign: "CHANGEME".to_owned(), ssid: 0, icon: 0, felinet: Default::default(), beacon: Default::default(), tunnel: Default::default(), } } } const CONFIGFILE : &str = "node-config.toml"; impl Config { pub fn load() -> anyhow::Result { if std::path::Path::new(CONFIGFILE).exists() { let file_contents = fs::read_to_string(CONFIGFILE)?; toml::from_str(&file_contents).context("parsing config file") } else { Ok(Default::default()) } } pub fn store(&self) -> anyhow::Result<()> { fs::write(CONFIGFILE, toml::to_string_pretty(&self)?) .context("writing config file") } }