aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: 7700b4573b941e3e55965e37300041c5812b642a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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<f64>,
    pub longitude: Option<f64>,
    pub altitude: Option<f64>,
    pub comment: Option<String>,
    pub antenna_height: Option<u8>,
    pub antenna_gain: Option<f32>,
    pub tx_power: Option<f32>, // 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<Self> {
        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")
    }
}