Added Network trait that keeps track of network settings and has DHCP/Manual options for setting up options
This commit is contained in:
parent
7dd4c04fe4
commit
fd9e861dde
7 changed files with 145 additions and 21 deletions
49
src/network/mod.rs
Normal file
49
src/network/mod.rs
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
mod dhcp;
|
||||
mod manual;
|
||||
|
||||
use crate::{MacAddress,IpAddress};
|
||||
use crate::bus::ActiveBus;
|
||||
use crate::register;
|
||||
pub use self::dhcp::Dhcp;
|
||||
pub use self::manual::Manual;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct NetworkSettings {
|
||||
mac: MacAddress,
|
||||
ip: IpAddress,
|
||||
gateway: IpAddress,
|
||||
subnet: IpAddress,
|
||||
}
|
||||
|
||||
pub trait Network {
|
||||
/// Gets (if necessary) and sets the network settings on the chip
|
||||
fn refresh<SpiBus: ActiveBus>(&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: ActiveBus>(bus: &mut SpiBus, current: &mut NetworkSettings, settings: &NetworkSettings) -> Result<(), SpiBus::Error> {
|
||||
if settings.gateway != current.gateway {
|
||||
let mut address = settings.gateway.address;
|
||||
block!(bus.transfer_frame(register::COMMON, register::common::GATEWAY, true, &mut address))?;
|
||||
current.gateway = settings.gateway;
|
||||
}
|
||||
if settings.subnet != current.subnet {
|
||||
let mut address = settings.subnet.address;
|
||||
block!(bus.transfer_frame(register::COMMON, register::common::SUBNET_MASK, true, &mut address))?;
|
||||
current.subnet = settings.subnet;
|
||||
}
|
||||
if settings.mac != current.mac {
|
||||
let mut address = settings.mac.address;
|
||||
block!(bus.transfer_frame(register::COMMON, register::common::MAC, true, &mut address))?;
|
||||
current.mac = settings.mac;
|
||||
}
|
||||
if settings.ip != current.ip {
|
||||
let mut address = settings.ip.address;
|
||||
block!(bus.transfer_frame(register::COMMON, register::common::IP, true, &mut address))?;
|
||||
current.ip = settings.ip;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue