From dcfa65509ebb7ee11f3f75c7cda533569d7e6e24 Mon Sep 17 00:00:00 2001 From: Jonah Dahlquist Date: Mon, 23 Nov 2020 22:56:10 -0800 Subject: [PATCH] Renamed Network mod to Host, removed debugging function, communted out dormant portions of DHCP host config --- src/device.rs | 39 +++++++++++++++++++-------------- src/host/dhcp.rs | 30 +++++++++++++++++++++++++ src/{network => host}/manual.rs | 15 ++++++------- src/{network => host}/mod.rs | 12 +++++----- src/inactive_device.rs | 32 ++++++++++++--------------- src/interface.rs | 28 +++++++++++------------ src/lib.rs | 2 ++ src/network/dhcp.rs | 30 ------------------------- src/socket.rs | 6 ----- src/udp.rs | 10 ++++----- src/uninitialized_device.rs | 24 ++++++++++---------- 11 files changed, 112 insertions(+), 116 deletions(-) create mode 100644 src/host/dhcp.rs rename src/{network => host}/manual.rs (67%) rename src/{network => host}/mod.rs (88%) delete mode 100644 src/network/dhcp.rs diff --git a/src/device.rs b/src/device.rs index 833ed4c..d840a22 100644 --- a/src/device.rs +++ b/src/device.rs @@ -4,14 +4,19 @@ use bit_field::BitArray; use bus::{ActiveBus, ActiveFourWire, ActiveThreeWire, FourWire, ThreeWire}; use embedded_hal::digital::v2::OutputPin; use embedded_hal::spi::FullDuplex; + use interface::Interface; use network::Network; -use register; -use socket::Socket; +use crate::bus::{ActiveBus, ActiveFourWire, ActiveThreeWire, FourWire, ThreeWire}; +use crate::host::Host; +use crate::inactive_device::InactiveDevice; +use crate::register; +use crate::socket::Socket; +use crate::uninitialized_device::UninitializedDevice; -pub struct Device { +pub struct Device { pub bus: SpiBus, - network: NetworkImpl, + host: HostImpl, sockets: [u8; 1], } @@ -26,11 +31,11 @@ impl From for ResetError { } } -impl Device { - pub fn new(bus: SpiBus, network: NetworkImpl) -> Self { +impl Device { + pub fn new(bus: SpiBus, host: HostImpl) -> Self { Device { bus, - network, + host, sockets: [0b11111111], } } @@ -69,7 +74,7 @@ impl Device { Ok(phy[0].into()) } - pub fn into_interface(self) -> Interface { + pub fn into_interface(self) -> Interface { self.into() } @@ -77,23 +82,23 @@ impl Device { self.sockets.set_bit(socket.index.into(), true); } - pub fn release(self) -> (SpiBus, NetworkImpl) { - (self.bus, self.network) + pub fn release(self) -> (SpiBus, HostImpl) { + (self.bus, self.host) } } -impl, ChipSelect: OutputPin, NetworkImpl: Network> - Device, NetworkImpl> +impl, ChipSelect: OutputPin, HostImpl: Host> + Device, HostImpl> { - pub fn deactivate(self) -> (InactiveDevice, NetworkImpl>, Spi) { + pub fn deactivate(self) -> (InactiveDevice, HostImpl>, Spi) { let (bus, spi) = self.bus.deactivate(); - (InactiveDevice::new(bus, self.network), spi) + (InactiveDevice::new(bus, self.host), spi) } } -impl, NetworkImpl: Network> Device, NetworkImpl> { - pub fn deactivate(self) -> (InactiveDevice, Spi) { +impl, HostImpl: Host> Device, HostImpl> { + pub fn deactivate(self) -> (InactiveDevice, Spi) { let (bus, spi) = self.bus.deactivate(); - (InactiveDevice::new(bus, self.network), spi) + (InactiveDevice::new(bus, self.host), spi) } } diff --git a/src/host/dhcp.rs b/src/host/dhcp.rs new file mode 100644 index 0000000..dedf226 --- /dev/null +++ b/src/host/dhcp.rs @@ -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(&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(()) + } +} diff --git a/src/network/manual.rs b/src/host/manual.rs similarity index 67% rename from src/network/manual.rs rename to src/host/manual.rs index 00c9099..ae07216 100644 --- a/src/network/manual.rs +++ b/src/host/manual.rs @@ -1,32 +1,31 @@ use crate::bus::ActiveBus; -use crate::network::Network; -use crate::network::NetworkSettings; +use crate::host::{Host, HostConfig}; use crate::MacAddress; use embedded_nal::Ipv4Addr; pub struct Manual { is_setup: bool, - settings: NetworkSettings, - current: NetworkSettings, + settings: HostConfig, + current: HostConfig, } impl Manual { pub fn new(mac: MacAddress, ip: Ipv4Addr, gateway: Ipv4Addr, subnet: Ipv4Addr) -> Self { Self { is_setup: false, - settings: NetworkSettings { + settings: HostConfig { mac, ip, gateway, subnet, }, - current: NetworkSettings::default(), + current: HostConfig::default(), } } } -impl Network for Manual { - /// Gets (if necessary) and sets the network settings on the chip +impl Host for Manual { + /// Gets (if necessary) and sets the host settings on the chip fn refresh(&mut self, bus: &mut SpiBus) -> Result<(), SpiBus::Error> { if !self.is_setup { Self::write_settings(bus, &mut self.current, &self.settings)?; diff --git a/src/network/mod.rs b/src/host/mod.rs similarity index 88% rename from src/network/mod.rs rename to src/host/mod.rs index c920b0a..5f50290 100644 --- a/src/network/mod.rs +++ b/src/host/mod.rs @@ -8,14 +8,14 @@ use crate::register; use crate::MacAddress; use embedded_nal::Ipv4Addr; -pub struct NetworkSettings { +pub struct HostConfig { mac: MacAddress, ip: Ipv4Addr, gateway: Ipv4Addr, subnet: Ipv4Addr, } -impl Default for NetworkSettings { +impl Default for HostConfig { fn default() -> Self { Self { mac: MacAddress::default(), @@ -26,8 +26,8 @@ impl Default for NetworkSettings { } } -pub trait Network { - /// Gets (if necessary) and sets the network settings on the chip +pub trait Host { + /// Gets (if necessary) and sets the host settings on the chip fn refresh(&mut self, bus: &mut SpiBus) -> Result<(), SpiBus::Error>; /// Write changed settings to chip @@ -36,8 +36,8 @@ pub trait Network { /// with any changes. fn write_settings( bus: &mut SpiBus, - current: &mut NetworkSettings, - settings: &NetworkSettings, + current: &mut HostConfig, + settings: &HostConfig, ) -> Result<(), SpiBus::Error> { if settings.gateway != current.gateway { let address = settings.gateway.octets(); diff --git a/src/inactive_device.rs b/src/inactive_device.rs index 39207c5..c774598 100644 --- a/src/inactive_device.rs +++ b/src/inactive_device.rs @@ -1,36 +1,32 @@ -use bus::{ActiveFourWire, ActiveThreeWire, Bus, FourWire, ThreeWire}; -use device::Device; +use crate::bus::{ActiveFourWire, ActiveThreeWire, Bus, FourWire, ThreeWire}; +use crate::device::Device; +use crate::host::Host; use embedded_hal::digital::v2::OutputPin; use embedded_hal::spi::FullDuplex; use network::Network; -pub struct InactiveDevice { +pub struct InactiveDevice { bus: SpiBus, - network: NetworkImpl, + host: HostImpl, } -impl InactiveDevice { - pub fn new(bus: SpiBus, network: NetworkImpl) -> Self { - Self { bus, network } +impl InactiveDevice { + pub fn new(bus: SpiBus, host: HostImpl) -> Self { + Self { bus, host } } } -impl - InactiveDevice, NetworkImpl> -{ +impl InactiveDevice, HostImpl> { pub fn activate>( self, spi: Spi, - ) -> Device, NetworkImpl> { - Device::new(self.bus.activate(spi), self.network) + ) -> Device, HostImpl> { + Device::new(self.bus.activate(spi), self.host) } } -impl InactiveDevice { - pub fn activate>( - self, - spi: Spi, - ) -> Device, NetworkImpl> { - Device::new(self.bus.activate(spi), self.network) +impl InactiveDevice { + pub fn activate>(self, spi: Spi) -> Device, HostImpl> { + Device::new(self.bus.activate(spi), self.host) } } diff --git a/src/interface.rs b/src/interface.rs index b1c731d..1ab90c6 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -2,26 +2,24 @@ use embedded_hal::digital::v2::OutputPin; use embedded_hal::spi::FullDuplex; 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 bus::{ActiveBus, ActiveFourWire, FourWire}; -use core::cell::RefCell; -use device::Device; -use network::{Manual, Network}; -use uninitialized_device::{InitializeError, UninitializedDevice}; - -pub struct Interface { - pub device: RefCell>, +pub struct Interface { + pub device: RefCell>, } -impl Interface { - fn new(device: Device) -> Self { +impl Interface { + fn new(device: Device) -> Self { Self { device: RefCell::new(device), } } - pub fn release(self) -> Device { + pub fn release(self) -> Device { self.device.into_inner() } } @@ -41,10 +39,10 @@ impl, ChipSelect: OutputPin> } } -impl From> - for Interface +impl From> + for Interface { - fn from(device: Device) -> Interface { - Interface::::new(device) + fn from(device: Device) -> Interface { + Interface::::new(device) } } diff --git a/src/lib.rs b/src/lib.rs index 8acc741..2a7757a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -77,9 +77,11 @@ impl Default for Mode { pub mod bus; mod device; +mod host; mod inactive_device; pub mod interface; mod network; +pub mod net; pub mod register; mod socket; mod udp; diff --git a/src/network/dhcp.rs b/src/network/dhcp.rs deleted file mode 100644 index f9ba1d2..0000000 --- a/src/network/dhcp.rs +++ /dev/null @@ -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(&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(()) - } -} diff --git a/src/socket.rs b/src/socket.rs index 9eed73a..1e97b10 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -192,10 +192,4 @@ impl Socket { } } } - - pub fn dump_register(&self, bus: &mut SpiBus) -> [u8; 0x30] { - let mut register = [0u8; 0x30]; - bus.read_frame(self.register(), 0u16, &mut register); - register - } } diff --git a/src/udp.rs b/src/udp.rs index 068301d..240658c 100644 --- a/src/udp.rs +++ b/src/udp.rs @@ -1,6 +1,6 @@ use crate::bus::ActiveBus; +use crate::host::Host; use crate::interface::Interface; -use crate::network::Network; use crate::register::socketn; use crate::socket::Socket; use core::fmt::Debug; @@ -175,10 +175,10 @@ impl From> for nb::Error { } } -impl UdpClient for Interface +impl UdpClient for Interface where SpiBus: ActiveBus, - NetworkImpl: Network, + HostImpl: Host, { type UdpSocket = UdpSocket; type Error = UdpSocketError; @@ -216,10 +216,10 @@ where } } -impl UdpServer for Interface +impl UdpServer for Interface where SpiBus: ActiveBus, - NetworkImpl: Network, + HostImpl: Host, { fn bind(&self, local_port: u16) -> Result { let mut device = self.device.borrow_mut(); diff --git a/src/uninitialized_device.rs b/src/uninitialized_device.rs index 8786afe..dadd5fb 100644 --- a/src/uninitialized_device.rs +++ b/src/uninitialized_device.rs @@ -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 bus::{ActiveBus, ActiveFourWire, ActiveThreeWire}; use device::Device; @@ -28,8 +31,8 @@ impl UninitializedDevice { mac: MacAddress, mode_options: Mode, ) -> Result, InitializeError> { - let network = Dhcp::new(mac); - self.initialize_with_network(network, mode_options) + let host = Dhcp::new(mac); + self.initialize_with_host(host, mode_options) } pub fn initialize_manual( @@ -53,15 +56,15 @@ impl UninitializedDevice { subnet: Ipv4Addr, mode_options: Mode, ) -> Result, InitializeError> { - let network = Manual::new(mac, ip, gateway, subnet); - self.initialize_with_network(network, mode_options) + let host = Manual::new(mac, ip, gateway, subnet); + self.initialize_with_host(host, mode_options) } - fn initialize_with_network( + fn initialize_with_host( mut self, - mut network: NetworkImpl, + mut host: HostImpl, mode_options: Mode, - ) -> Result, InitializeError> { + ) -> Result, InitializeError> { self.assert_chip_version(0x4)?; // RESET @@ -72,10 +75,9 @@ impl UninitializedDevice { self.set_mode(mode_options) .map_err(InitializeError::SpiError)?; - network - .refresh(&mut self.bus) + host.refresh(&mut self.bus) .map_err(InitializeError::SpiError)?; - Ok(Device::new(self.bus, network)) + Ok(Device::new(self.bus, host)) } fn assert_chip_version(