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
mod dhcp;
mod manual;

pub use self::dhcp::Dhcp;
pub use self::manual::Manual;
use crate::bus::Bus;
use crate::register;
use crate::MacAddress;
use embedded_nal::Ipv4Addr;

pub struct HostConfig {
    mac: MacAddress,
    ip: Ipv4Addr,
    gateway: Ipv4Addr,
    subnet: Ipv4Addr,
}

impl Default for HostConfig {
    fn default() -> Self {
        Self {
            mac: MacAddress::default(),
            ip: Ipv4Addr::unspecified(),
            gateway: Ipv4Addr::unspecified(),
            subnet: Ipv4Addr::unspecified(),
        }
    }
}

pub trait Host {
    /// Gets (if necessary) and sets the host settings on the chip
    fn refresh<SpiBus: Bus>(&mut self, bus: &mut SpiBus) -> Result<(), SpiBus::Error>;

    /// Write changed settings to chip
    ///
    /// Will check all settings and write any new ones to the chip.  Will update the settings returned by `current`
    /// with any changes.
    fn write_settings<SpiBus: Bus>(
        bus: &mut SpiBus,
        current: &mut HostConfig,
        settings: &HostConfig,
    ) -> Result<(), SpiBus::Error> {
        if settings.gateway != current.gateway {
            let address = settings.gateway.octets();
            bus.write_frame(register::COMMON, register::common::GATEWAY, &address)?;
            current.gateway = settings.gateway;
        }
        if settings.subnet != current.subnet {
            let address = settings.subnet.octets();
            bus.write_frame(register::COMMON, register::common::SUBNET_MASK, &address)?;
            current.subnet = settings.subnet;
        }
        if settings.mac != current.mac {
            let address = settings.mac.octets;
            bus.write_frame(register::COMMON, register::common::MAC, &address)?;
            current.mac = settings.mac;
        }
        if settings.ip != current.ip {
            let address = settings.ip.octets();
            bus.write_frame(register::COMMON, register::common::IP, &address)?;
            current.ip = settings.ip;
        }
        Ok(())
    }
}