Renamed Network mod to Host, removed debugging function, communted out dormant portions of DHCP host config

This commit is contained in:
Jonah Dahlquist 2020-11-23 22:56:10 -08:00
commit dcfa65509e
11 changed files with 112 additions and 116 deletions

View file

@ -4,14 +4,19 @@ use bit_field::BitArray;
use bus::{ActiveBus, ActiveFourWire, ActiveThreeWire, FourWire, ThreeWire}; use bus::{ActiveBus, ActiveFourWire, ActiveThreeWire, FourWire, ThreeWire};
use embedded_hal::digital::v2::OutputPin; use embedded_hal::digital::v2::OutputPin;
use embedded_hal::spi::FullDuplex; use embedded_hal::spi::FullDuplex;
use interface::Interface; use interface::Interface;
use network::Network; use network::Network;
use register; use crate::bus::{ActiveBus, ActiveFourWire, ActiveThreeWire, FourWire, ThreeWire};
use socket::Socket; use crate::host::Host;
use crate::inactive_device::InactiveDevice;
use crate::register;
use crate::socket::Socket;
use crate::uninitialized_device::UninitializedDevice;
pub struct Device<SpiBus: ActiveBus, NetworkImpl: Network> { pub struct Device<SpiBus: ActiveBus, HostImpl: Host> {
pub bus: SpiBus, pub bus: SpiBus,
network: NetworkImpl, host: HostImpl,
sockets: [u8; 1], sockets: [u8; 1],
} }
@ -26,11 +31,11 @@ impl<E> From<E> for ResetError<E> {
} }
} }
impl<SpiBus: ActiveBus, NetworkImpl: Network> Device<SpiBus, NetworkImpl> { impl<SpiBus: ActiveBus, HostImpl: Host> Device<SpiBus, HostImpl> {
pub fn new(bus: SpiBus, network: NetworkImpl) -> Self { pub fn new(bus: SpiBus, host: HostImpl) -> Self {
Device { Device {
bus, bus,
network, host,
sockets: [0b11111111], sockets: [0b11111111],
} }
} }
@ -69,7 +74,7 @@ impl<SpiBus: ActiveBus, NetworkImpl: Network> Device<SpiBus, NetworkImpl> {
Ok(phy[0].into()) Ok(phy[0].into())
} }
pub fn into_interface(self) -> Interface<SpiBus, NetworkImpl> { pub fn into_interface(self) -> Interface<SpiBus, HostImpl> {
self.into() self.into()
} }
@ -77,23 +82,23 @@ impl<SpiBus: ActiveBus, NetworkImpl: Network> Device<SpiBus, NetworkImpl> {
self.sockets.set_bit(socket.index.into(), true); self.sockets.set_bit(socket.index.into(), true);
} }
pub fn release(self) -> (SpiBus, NetworkImpl) { pub fn release(self) -> (SpiBus, HostImpl) {
(self.bus, self.network) (self.bus, self.host)
} }
} }
impl<Spi: FullDuplex<u8>, ChipSelect: OutputPin, NetworkImpl: Network> impl<Spi: FullDuplex<u8>, ChipSelect: OutputPin, HostImpl: Host>
Device<ActiveFourWire<Spi, ChipSelect>, NetworkImpl> Device<ActiveFourWire<Spi, ChipSelect>, HostImpl>
{ {
pub fn deactivate(self) -> (InactiveDevice<FourWire<ChipSelect>, NetworkImpl>, Spi) { pub fn deactivate(self) -> (InactiveDevice<FourWire<ChipSelect>, HostImpl>, Spi) {
let (bus, spi) = self.bus.deactivate(); let (bus, spi) = self.bus.deactivate();
(InactiveDevice::new(bus, self.network), spi) (InactiveDevice::new(bus, self.host), spi)
} }
} }
impl<Spi: FullDuplex<u8>, NetworkImpl: Network> Device<ActiveThreeWire<Spi>, NetworkImpl> { impl<Spi: FullDuplex<u8>, HostImpl: Host> Device<ActiveThreeWire<Spi>, HostImpl> {
pub fn deactivate(self) -> (InactiveDevice<ThreeWire, NetworkImpl>, Spi) { pub fn deactivate(self) -> (InactiveDevice<ThreeWire, HostImpl>, Spi) {
let (bus, spi) = self.bus.deactivate(); let (bus, spi) = self.bus.deactivate();
(InactiveDevice::new(bus, self.network), spi) (InactiveDevice::new(bus, self.host), spi)
} }
} }

30
src/host/dhcp.rs Normal file
View file

@ -0,0 +1,30 @@
use crate::bus::ActiveBus;
use crate::host::Host;
use crate::MacAddress;
pub struct Dhcp {
// settings: HostConfig,
// current: HostConfig,
}
impl Dhcp {
pub fn new(_mac: MacAddress) -> Self {
// let settings = HostConfig {
// mac,
// ..HostConfig::default()
// };
Self {
// settings,
// current: HostConfig::default(),
}
}
}
impl Host for Dhcp {
/// Gets (if necessary) and sets the host settings on the chip
fn refresh<SpiBus: ActiveBus>(&mut self, _bus: &mut SpiBus) -> Result<(), SpiBus::Error> {
// TODO actually negotiate settings from DHCP
// TODO figure out how should receive socket for DHCP negotiations
Ok(())
}
}

View file

@ -1,32 +1,31 @@
use crate::bus::ActiveBus; use crate::bus::ActiveBus;
use crate::network::Network; use crate::host::{Host, HostConfig};
use crate::network::NetworkSettings;
use crate::MacAddress; use crate::MacAddress;
use embedded_nal::Ipv4Addr; use embedded_nal::Ipv4Addr;
pub struct Manual { pub struct Manual {
is_setup: bool, is_setup: bool,
settings: NetworkSettings, settings: HostConfig,
current: NetworkSettings, current: HostConfig,
} }
impl Manual { impl Manual {
pub fn new(mac: MacAddress, ip: Ipv4Addr, gateway: Ipv4Addr, subnet: Ipv4Addr) -> Self { pub fn new(mac: MacAddress, ip: Ipv4Addr, gateway: Ipv4Addr, subnet: Ipv4Addr) -> Self {
Self { Self {
is_setup: false, is_setup: false,
settings: NetworkSettings { settings: HostConfig {
mac, mac,
ip, ip,
gateway, gateway,
subnet, subnet,
}, },
current: NetworkSettings::default(), current: HostConfig::default(),
} }
} }
} }
impl Network for Manual { impl Host for Manual {
/// Gets (if necessary) and sets the network settings on the chip /// Gets (if necessary) and sets the host settings on the chip
fn refresh<SpiBus: ActiveBus>(&mut self, bus: &mut SpiBus) -> Result<(), SpiBus::Error> { fn refresh<SpiBus: ActiveBus>(&mut self, bus: &mut SpiBus) -> Result<(), SpiBus::Error> {
if !self.is_setup { if !self.is_setup {
Self::write_settings(bus, &mut self.current, &self.settings)?; Self::write_settings(bus, &mut self.current, &self.settings)?;

View file

@ -8,14 +8,14 @@ use crate::register;
use crate::MacAddress; use crate::MacAddress;
use embedded_nal::Ipv4Addr; use embedded_nal::Ipv4Addr;
pub struct NetworkSettings { pub struct HostConfig {
mac: MacAddress, mac: MacAddress,
ip: Ipv4Addr, ip: Ipv4Addr,
gateway: Ipv4Addr, gateway: Ipv4Addr,
subnet: Ipv4Addr, subnet: Ipv4Addr,
} }
impl Default for NetworkSettings { impl Default for HostConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
mac: MacAddress::default(), mac: MacAddress::default(),
@ -26,8 +26,8 @@ impl Default for NetworkSettings {
} }
} }
pub trait Network { pub trait Host {
/// Gets (if necessary) and sets the network settings on the chip /// Gets (if necessary) and sets the host settings on the chip
fn refresh<SpiBus: ActiveBus>(&mut self, bus: &mut SpiBus) -> Result<(), SpiBus::Error>; fn refresh<SpiBus: ActiveBus>(&mut self, bus: &mut SpiBus) -> Result<(), SpiBus::Error>;
/// Write changed settings to chip /// Write changed settings to chip
@ -36,8 +36,8 @@ pub trait Network {
/// with any changes. /// with any changes.
fn write_settings<SpiBus: ActiveBus>( fn write_settings<SpiBus: ActiveBus>(
bus: &mut SpiBus, bus: &mut SpiBus,
current: &mut NetworkSettings, current: &mut HostConfig,
settings: &NetworkSettings, settings: &HostConfig,
) -> Result<(), SpiBus::Error> { ) -> Result<(), SpiBus::Error> {
if settings.gateway != current.gateway { if settings.gateway != current.gateway {
let address = settings.gateway.octets(); let address = settings.gateway.octets();

View file

@ -1,36 +1,32 @@
use bus::{ActiveFourWire, ActiveThreeWire, Bus, FourWire, ThreeWire}; use crate::bus::{ActiveFourWire, ActiveThreeWire, Bus, FourWire, ThreeWire};
use device::Device; use crate::device::Device;
use crate::host::Host;
use embedded_hal::digital::v2::OutputPin; use embedded_hal::digital::v2::OutputPin;
use embedded_hal::spi::FullDuplex; use embedded_hal::spi::FullDuplex;
use network::Network; use network::Network;
pub struct InactiveDevice<SpiBus: Bus, NetworkImpl: Network> { pub struct InactiveDevice<SpiBus: Bus, HostImpl: Host> {
bus: SpiBus, bus: SpiBus,
network: NetworkImpl, host: HostImpl,
} }
impl<SpiBus: Bus, NetworkImpl: Network> InactiveDevice<SpiBus, NetworkImpl> { impl<SpiBus: Bus, HostImpl: Host> InactiveDevice<SpiBus, HostImpl> {
pub fn new(bus: SpiBus, network: NetworkImpl) -> Self { pub fn new(bus: SpiBus, host: HostImpl) -> Self {
Self { bus, network } Self { bus, host }
} }
} }
impl<ChipSelect: OutputPin, NetworkImpl: Network> impl<ChipSelect: OutputPin, HostImpl: Host> InactiveDevice<FourWire<ChipSelect>, HostImpl> {
InactiveDevice<FourWire<ChipSelect>, NetworkImpl>
{
pub fn activate<Spi: FullDuplex<u8>>( pub fn activate<Spi: FullDuplex<u8>>(
self, self,
spi: Spi, spi: Spi,
) -> Device<ActiveFourWire<Spi, ChipSelect>, NetworkImpl> { ) -> Device<ActiveFourWire<Spi, ChipSelect>, HostImpl> {
Device::new(self.bus.activate(spi), self.network) Device::new(self.bus.activate(spi), self.host)
} }
} }
impl<NetworkImpl: Network> InactiveDevice<ThreeWire, NetworkImpl> { impl<HostImpl: Host> InactiveDevice<ThreeWire, HostImpl> {
pub fn activate<Spi: FullDuplex<u8>>( pub fn activate<Spi: FullDuplex<u8>>(self, spi: Spi) -> Device<ActiveThreeWire<Spi>, HostImpl> {
self, Device::new(self.bus.activate(spi), self.host)
spi: Spi,
) -> Device<ActiveThreeWire<Spi>, NetworkImpl> {
Device::new(self.bus.activate(spi), self.network)
} }
} }

View file

@ -2,26 +2,24 @@ use embedded_hal::digital::v2::OutputPin;
use embedded_hal::spi::FullDuplex; use embedded_hal::spi::FullDuplex;
use embedded_nal::Ipv4Addr; use embedded_nal::Ipv4Addr;
use crate::bus::{ActiveBus, ActiveFourWire, FourWire};
use crate::device::Device;
use crate::host::{Host, Manual};
use crate::uninitialized_device::{InitializeError, UninitializedDevice};
use crate::{MacAddress, Mode}; use crate::{MacAddress, Mode};
use bus::{ActiveBus, ActiveFourWire, FourWire}; pub struct Interface<SpiBus: ActiveBus, HostImpl: Host> {
use core::cell::RefCell; pub device: RefCell<Device<SpiBus, HostImpl>>,
use device::Device;
use network::{Manual, Network};
use uninitialized_device::{InitializeError, UninitializedDevice};
pub struct Interface<SpiBus: ActiveBus, NetworkImpl: Network> {
pub device: RefCell<Device<SpiBus, NetworkImpl>>,
} }
impl<SpiBus: ActiveBus, NetworkImpl: Network> Interface<SpiBus, NetworkImpl> { impl<SpiBus: ActiveBus, HostImpl: Host> Interface<SpiBus, HostImpl> {
fn new(device: Device<SpiBus, NetworkImpl>) -> Self { fn new(device: Device<SpiBus, HostImpl>) -> Self {
Self { Self {
device: RefCell::new(device), device: RefCell::new(device),
} }
} }
pub fn release(self) -> Device<SpiBus, NetworkImpl> { pub fn release(self) -> Device<SpiBus, HostImpl> {
self.device.into_inner() self.device.into_inner()
} }
} }
@ -41,10 +39,10 @@ impl<Spi: FullDuplex<u8>, ChipSelect: OutputPin>
} }
} }
impl<SpiBus: ActiveBus, NetworkImpl: Network> From<Device<SpiBus, NetworkImpl>> impl<SpiBus: ActiveBus, HostImpl: Host> From<Device<SpiBus, HostImpl>>
for Interface<SpiBus, NetworkImpl> for Interface<SpiBus, HostImpl>
{ {
fn from(device: Device<SpiBus, NetworkImpl>) -> Interface<SpiBus, NetworkImpl> { fn from(device: Device<SpiBus, HostImpl>) -> Interface<SpiBus, HostImpl> {
Interface::<SpiBus, NetworkImpl>::new(device) Interface::<SpiBus, HostImpl>::new(device)
} }
} }

View file

@ -77,9 +77,11 @@ impl Default for Mode {
pub mod bus; pub mod bus;
mod device; mod device;
mod host;
mod inactive_device; mod inactive_device;
pub mod interface; pub mod interface;
mod network; mod network;
pub mod net;
pub mod register; pub mod register;
mod socket; mod socket;
mod udp; mod udp;

View file

@ -1,30 +0,0 @@
use crate::bus::ActiveBus;
use crate::network::{Network, NetworkSettings};
use crate::MacAddress;
pub struct Dhcp {
settings: NetworkSettings,
current: NetworkSettings,
}
impl Dhcp {
pub fn new(mac: MacAddress) -> Self {
let settings = NetworkSettings {
mac,
..NetworkSettings::default()
};
Self {
settings,
current: NetworkSettings::default(),
}
}
}
impl Network for Dhcp {
/// Gets (if necessary) and sets the network settings on the chip
fn refresh<SpiBus: ActiveBus>(&mut self, _bus: &mut SpiBus) -> Result<(), SpiBus::Error> {
// TODO actually negotiate settings from DHCP
// TODO figure out how should receive socket for DHCP negotiations
Ok(())
}
}

View file

@ -192,10 +192,4 @@ impl Socket {
} }
} }
} }
pub fn dump_register<SpiBus: ActiveBus>(&self, bus: &mut SpiBus) -> [u8; 0x30] {
let mut register = [0u8; 0x30];
bus.read_frame(self.register(), 0u16, &mut register);
register
}
} }

View file

@ -1,6 +1,6 @@
use crate::bus::ActiveBus; use crate::bus::ActiveBus;
use crate::host::Host;
use crate::interface::Interface; use crate::interface::Interface;
use crate::network::Network;
use crate::register::socketn; use crate::register::socketn;
use crate::socket::Socket; use crate::socket::Socket;
use core::fmt::Debug; use core::fmt::Debug;
@ -175,10 +175,10 @@ impl<E: Debug> From<NbError<E>> for nb::Error<E> {
} }
} }
impl<SpiBus, NetworkImpl> UdpClient for Interface<SpiBus, NetworkImpl> impl<SpiBus, HostImpl> UdpClient for Interface<SpiBus, HostImpl>
where where
SpiBus: ActiveBus, SpiBus: ActiveBus,
NetworkImpl: Network, HostImpl: Host,
{ {
type UdpSocket = UdpSocket; type UdpSocket = UdpSocket;
type Error = UdpSocketError<SpiBus::Error>; type Error = UdpSocketError<SpiBus::Error>;
@ -216,10 +216,10 @@ where
} }
} }
impl<SpiBus, NetworkImpl> UdpServer for Interface<SpiBus, NetworkImpl> impl<SpiBus, HostImpl> UdpServer for Interface<SpiBus, HostImpl>
where where
SpiBus: ActiveBus, SpiBus: ActiveBus,
NetworkImpl: Network, HostImpl: Host,
{ {
fn bind(&self, local_port: u16) -> Result<Self::UdpSocket, Self::Error> { fn bind(&self, local_port: u16) -> Result<Self::UdpSocket, Self::Error> {
let mut device = self.device.borrow_mut(); let mut device = self.device.borrow_mut();

View file

@ -1,4 +1,7 @@
use crate::network::{Dhcp, Manual, Network}; use crate::bus::{ActiveBus, ActiveFourWire, ActiveThreeWire};
use crate::device::Device;
use crate::host::{Dhcp, Host, Manual};
use crate::register;
use crate::{MacAddress, Mode}; use crate::{MacAddress, Mode};
use bus::{ActiveBus, ActiveFourWire, ActiveThreeWire}; use bus::{ActiveBus, ActiveFourWire, ActiveThreeWire};
use device::Device; use device::Device;
@ -28,8 +31,8 @@ impl<SpiBus: ActiveBus> UninitializedDevice<SpiBus> {
mac: MacAddress, mac: MacAddress,
mode_options: Mode, mode_options: Mode,
) -> Result<Device<SpiBus, Dhcp>, InitializeError<SpiBus::Error>> { ) -> Result<Device<SpiBus, Dhcp>, InitializeError<SpiBus::Error>> {
let network = Dhcp::new(mac); let host = Dhcp::new(mac);
self.initialize_with_network(network, mode_options) self.initialize_with_host(host, mode_options)
} }
pub fn initialize_manual( pub fn initialize_manual(
@ -53,15 +56,15 @@ impl<SpiBus: ActiveBus> UninitializedDevice<SpiBus> {
subnet: Ipv4Addr, subnet: Ipv4Addr,
mode_options: Mode, mode_options: Mode,
) -> Result<Device<SpiBus, Manual>, InitializeError<SpiBus::Error>> { ) -> Result<Device<SpiBus, Manual>, InitializeError<SpiBus::Error>> {
let network = Manual::new(mac, ip, gateway, subnet); let host = Manual::new(mac, ip, gateway, subnet);
self.initialize_with_network(network, mode_options) self.initialize_with_host(host, mode_options)
} }
fn initialize_with_network<NetworkImpl: Network>( fn initialize_with_host<HostImpl: Host>(
mut self, mut self,
mut network: NetworkImpl, mut host: HostImpl,
mode_options: Mode, mode_options: Mode,
) -> Result<Device<SpiBus, NetworkImpl>, InitializeError<SpiBus::Error>> { ) -> Result<Device<SpiBus, HostImpl>, InitializeError<SpiBus::Error>> {
self.assert_chip_version(0x4)?; self.assert_chip_version(0x4)?;
// RESET // RESET
@ -72,10 +75,9 @@ impl<SpiBus: ActiveBus> UninitializedDevice<SpiBus> {
self.set_mode(mode_options) self.set_mode(mode_options)
.map_err(InitializeError::SpiError)?; .map_err(InitializeError::SpiError)?;
network host.refresh(&mut self.bus)
.refresh(&mut self.bus)
.map_err(InitializeError::SpiError)?; .map_err(InitializeError::SpiError)?;
Ok(Device::new(self.bus, network)) Ok(Device::new(self.bus, host))
} }
fn assert_chip_version( fn assert_chip_version(