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 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<SpiBus: ActiveBus, NetworkImpl: Network> {
pub struct Device<SpiBus: ActiveBus, HostImpl: Host> {
pub bus: SpiBus,
network: NetworkImpl,
host: HostImpl,
sockets: [u8; 1],
}
@ -26,11 +31,11 @@ impl<E> From<E> for ResetError<E> {
}
}
impl<SpiBus: ActiveBus, NetworkImpl: Network> Device<SpiBus, NetworkImpl> {
pub fn new(bus: SpiBus, network: NetworkImpl) -> Self {
impl<SpiBus: ActiveBus, HostImpl: Host> Device<SpiBus, HostImpl> {
pub fn new(bus: SpiBus, host: HostImpl) -> Self {
Device {
bus,
network,
host,
sockets: [0b11111111],
}
}
@ -69,7 +74,7 @@ impl<SpiBus: ActiveBus, NetworkImpl: Network> Device<SpiBus, NetworkImpl> {
Ok(phy[0].into())
}
pub fn into_interface(self) -> Interface<SpiBus, NetworkImpl> {
pub fn into_interface(self) -> Interface<SpiBus, HostImpl> {
self.into()
}
@ -77,23 +82,23 @@ impl<SpiBus: ActiveBus, NetworkImpl: Network> Device<SpiBus, NetworkImpl> {
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<Spi: FullDuplex<u8>, ChipSelect: OutputPin, NetworkImpl: Network>
Device<ActiveFourWire<Spi, ChipSelect>, NetworkImpl>
impl<Spi: FullDuplex<u8>, ChipSelect: OutputPin, HostImpl: Host>
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();
(InactiveDevice::new(bus, self.network), spi)
(InactiveDevice::new(bus, self.host), spi)
}
}
impl<Spi: FullDuplex<u8>, NetworkImpl: Network> Device<ActiveThreeWire<Spi>, NetworkImpl> {
pub fn deactivate(self) -> (InactiveDevice<ThreeWire, NetworkImpl>, Spi) {
impl<Spi: FullDuplex<u8>, HostImpl: Host> Device<ActiveThreeWire<Spi>, HostImpl> {
pub fn deactivate(self) -> (InactiveDevice<ThreeWire, HostImpl>, Spi) {
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::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<SpiBus: ActiveBus>(&mut self, bus: &mut SpiBus) -> Result<(), SpiBus::Error> {
if !self.is_setup {
Self::write_settings(bus, &mut self.current, &self.settings)?;

View file

@ -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<SpiBus: ActiveBus>(&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<SpiBus: ActiveBus>(
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();

View file

@ -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<SpiBus: Bus, NetworkImpl: Network> {
pub struct InactiveDevice<SpiBus: Bus, HostImpl: Host> {
bus: SpiBus,
network: NetworkImpl,
host: HostImpl,
}
impl<SpiBus: Bus, NetworkImpl: Network> InactiveDevice<SpiBus, NetworkImpl> {
pub fn new(bus: SpiBus, network: NetworkImpl) -> Self {
Self { bus, network }
impl<SpiBus: Bus, HostImpl: Host> InactiveDevice<SpiBus, HostImpl> {
pub fn new(bus: SpiBus, host: HostImpl) -> Self {
Self { bus, host }
}
}
impl<ChipSelect: OutputPin, NetworkImpl: Network>
InactiveDevice<FourWire<ChipSelect>, NetworkImpl>
{
impl<ChipSelect: OutputPin, HostImpl: Host> InactiveDevice<FourWire<ChipSelect>, HostImpl> {
pub fn activate<Spi: FullDuplex<u8>>(
self,
spi: Spi,
) -> Device<ActiveFourWire<Spi, ChipSelect>, NetworkImpl> {
Device::new(self.bus.activate(spi), self.network)
) -> Device<ActiveFourWire<Spi, ChipSelect>, HostImpl> {
Device::new(self.bus.activate(spi), self.host)
}
}
impl<NetworkImpl: Network> InactiveDevice<ThreeWire, NetworkImpl> {
pub fn activate<Spi: FullDuplex<u8>>(
self,
spi: Spi,
) -> Device<ActiveThreeWire<Spi>, NetworkImpl> {
Device::new(self.bus.activate(spi), self.network)
impl<HostImpl: Host> InactiveDevice<ThreeWire, HostImpl> {
pub fn activate<Spi: FullDuplex<u8>>(self, spi: Spi) -> Device<ActiveThreeWire<Spi>, HostImpl> {
Device::new(self.bus.activate(spi), self.host)
}
}

View file

@ -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<SpiBus: ActiveBus, NetworkImpl: Network> {
pub device: RefCell<Device<SpiBus, NetworkImpl>>,
pub struct Interface<SpiBus: ActiveBus, HostImpl: Host> {
pub device: RefCell<Device<SpiBus, HostImpl>>,
}
impl<SpiBus: ActiveBus, NetworkImpl: Network> Interface<SpiBus, NetworkImpl> {
fn new(device: Device<SpiBus, NetworkImpl>) -> Self {
impl<SpiBus: ActiveBus, HostImpl: Host> Interface<SpiBus, HostImpl> {
fn new(device: Device<SpiBus, HostImpl>) -> Self {
Self {
device: RefCell::new(device),
}
}
pub fn release(self) -> Device<SpiBus, NetworkImpl> {
pub fn release(self) -> Device<SpiBus, HostImpl> {
self.device.into_inner()
}
}
@ -41,10 +39,10 @@ impl<Spi: FullDuplex<u8>, ChipSelect: OutputPin>
}
}
impl<SpiBus: ActiveBus, NetworkImpl: Network> From<Device<SpiBus, NetworkImpl>>
for Interface<SpiBus, NetworkImpl>
impl<SpiBus: ActiveBus, HostImpl: Host> From<Device<SpiBus, HostImpl>>
for Interface<SpiBus, HostImpl>
{
fn from(device: Device<SpiBus, NetworkImpl>) -> Interface<SpiBus, NetworkImpl> {
Interface::<SpiBus, NetworkImpl>::new(device)
fn from(device: Device<SpiBus, HostImpl>) -> Interface<SpiBus, HostImpl> {
Interface::<SpiBus, HostImpl>::new(device)
}
}

View file

@ -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;

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::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<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
SpiBus: ActiveBus,
NetworkImpl: Network,
HostImpl: Host,
{
type UdpSocket = UdpSocket;
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
SpiBus: ActiveBus,
NetworkImpl: Network,
HostImpl: Host,
{
fn bind(&self, local_port: u16) -> Result<Self::UdpSocket, Self::Error> {
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 bus::{ActiveBus, ActiveFourWire, ActiveThreeWire};
use device::Device;
@ -28,8 +31,8 @@ impl<SpiBus: ActiveBus> UninitializedDevice<SpiBus> {
mac: MacAddress,
mode_options: Mode,
) -> Result<Device<SpiBus, Dhcp>, InitializeError<SpiBus::Error>> {
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<SpiBus: ActiveBus> UninitializedDevice<SpiBus> {
subnet: Ipv4Addr,
mode_options: Mode,
) -> Result<Device<SpiBus, Manual>, InitializeError<SpiBus::Error>> {
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<NetworkImpl: Network>(
fn initialize_with_host<HostImpl: Host>(
mut self,
mut network: NetworkImpl,
mut host: HostImpl,
mode_options: Mode,
) -> Result<Device<SpiBus, NetworkImpl>, InitializeError<SpiBus::Error>> {
) -> Result<Device<SpiBus, HostImpl>, InitializeError<SpiBus::Error>> {
self.assert_chip_version(0x4)?;
// RESET
@ -72,10 +75,9 @@ impl<SpiBus: ActiveBus> UninitializedDevice<SpiBus> {
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(