Stubbed UdpSocket struct, added Socket structs

This commit is contained in:
Jonah Dahlquist 2019-08-13 20:03:02 -05:00 committed by Jonah Dahlquist
commit b30e4d0d34
7 changed files with 100 additions and 15 deletions

View file

@ -2,16 +2,22 @@ use bus::{ActiveFourWire, ActiveThreeWire, Bus, FourWire, ThreeWire};
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::spi::FullDuplex;
use network::Network;
use socket::OwnedSockets;
use w5500::W5500;
pub struct InactiveW5500<SpiBus: Bus, NetworkImpl: Network> {
bus: SpiBus,
network: NetworkImpl,
sockets: OwnedSockets,
}
impl<SpiBus: Bus, NetworkImpl: Network> InactiveW5500<SpiBus, NetworkImpl> {
pub fn new(bus: SpiBus, network: NetworkImpl) -> Self {
Self { bus, network }
pub fn new(bus: SpiBus, network: NetworkImpl, sockets: OwnedSockets) -> Self {
Self {
bus,
network,
sockets,
}
}
}
@ -20,7 +26,7 @@ impl<ChipSelect: OutputPin, NetworkImpl: Network> InactiveW5500<FourWire<ChipSel
self,
spi: Spi,
) -> W5500<ActiveFourWire<Spi, ChipSelect>, NetworkImpl> {
W5500::new(self.bus.activate(spi), self.network)
W5500::new(self.bus.activate(spi), self.network, self.sockets)
}
}
@ -29,6 +35,6 @@ impl<NetworkImpl: Network> InactiveW5500<ThreeWire, NetworkImpl> {
self,
spi: Spi,
) -> W5500<ActiveThreeWire<Spi>, NetworkImpl> {
W5500::new(self.bus.activate(spi), self.network)
W5500::new(self.bus.activate(spi), self.network, self.sockets)
}
}

View file

@ -940,9 +940,11 @@ impl Default for Mode {
// }
// }
pub mod bus;
pub mod inactive_w5500;
mod bus;
mod inactive_w5500;
mod network;
mod register;
mod socket;
mod udp;
pub mod uninitialized_w5500;
pub mod w5500;
mod w5500;

View file

@ -24,6 +24,7 @@ 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(())
}
}

39
src/socket/mod.rs Normal file
View file

@ -0,0 +1,39 @@
pub trait Socket {}
pub type OwnedSockets = (
Socket0,
Socket1,
Socket2,
Socket3,
Socket4,
Socket5,
Socket6,
Socket7,
);
pub type Sockets<'a> = (
&'a mut Socket0,
&'a mut Socket1,
&'a mut Socket2,
&'a mut Socket3,
&'a mut Socket4,
&'a mut Socket5,
&'a mut Socket6,
&'a mut Socket7,
);
pub struct Socket0 {}
impl Socket for Socket0 {}
pub struct Socket1 {}
impl Socket for Socket1 {}
pub struct Socket2 {}
impl Socket for Socket2 {}
pub struct Socket3 {}
impl Socket for Socket3 {}
pub struct Socket4 {}
impl Socket for Socket4 {}
pub struct Socket5 {}
impl Socket for Socket5 {}
pub struct Socket6 {}
impl Socket for Socket6 {}
pub struct Socket7 {}
impl Socket for Socket7 {}

5
src/udp/mod.rs Normal file
View file

@ -0,0 +1,5 @@
use crate::socket::Socket;
pub struct UdpSocket<SocketImpl: Socket> {
pub socket: SocketImpl,
}

View file

@ -4,6 +4,7 @@ use bus::{ActiveBus, ActiveFourWire, ActiveThreeWire};
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::spi::FullDuplex;
use register;
use socket::{Socket0, Socket1, Socket2, Socket3, Socket4, Socket5, Socket6, Socket7};
use w5500::W5500;
pub struct UninitializedW5500<SpiBus: ActiveBus> {
@ -64,8 +65,17 @@ impl<SpiBus: ActiveBus> UninitializedW5500<SpiBus> {
network
.refresh(&mut self.bus)
.map_err(|e| InitializeError::SpiError(e))?;
Ok(W5500::new(self.bus, network))
// TODO give ownership of all sockets
let sockets = (
Socket0 {},
Socket1 {},
Socket2 {},
Socket3 {},
Socket4 {},
Socket5 {},
Socket6 {},
Socket7 {},
);
Ok(W5500::new(self.bus, network, sockets))
}
fn assert_chip_version(

View file

@ -5,18 +5,38 @@ use embedded_hal::digital::v2::OutputPin;
use embedded_hal::spi::FullDuplex;
use network::Network;
use register;
use socket::{OwnedSockets, Socket, Sockets};
use udp::UdpSocket;
pub struct W5500<SpiBus: ActiveBus, NetworkImpl: Network> {
bus: SpiBus,
network: NetworkImpl,
sockets: OwnedSockets,
}
impl<SpiBus: ActiveBus, NetworkImpl: Network> W5500<SpiBus, NetworkImpl> {
pub fn new(bus: SpiBus, network: NetworkImpl) -> Self {
W5500 { bus, network }
pub fn new(bus: SpiBus, network: NetworkImpl, sockets: OwnedSockets) -> Self {
W5500 {
bus,
network,
sockets,
}
}
pub fn sockets(&mut self) -> Sockets {
(
&mut self.sockets.0,
&mut self.sockets.1,
&mut self.sockets.2,
&mut self.sockets.3,
&mut self.sockets.4,
&mut self.sockets.5,
&mut self.sockets.6,
&mut self.sockets.7,
)
}
pub fn reset(mut self) -> Result<UninitializedW5500<SpiBus>, SpiBus::Error> {
// TODO accept all sockets back
self.clear_mode()?;
Ok(UninitializedW5500::new(self.bus))
}
@ -30,7 +50,9 @@ impl<SpiBus: ActiveBus, NetworkImpl: Network> W5500<SpiBus, NetworkImpl> {
Ok(())
}
//TODO open_udp_socket
pub fn open_udp_socket<SocketImpl: Socket>(&self, socket: SocketImpl) -> UdpSocket<SocketImpl> {
UdpSocket { socket }
}
}
impl<Spi: FullDuplex<u8>, ChipSelect: OutputPin, NetworkImpl: Network>
@ -38,13 +60,13 @@ impl<Spi: FullDuplex<u8>, ChipSelect: OutputPin, NetworkImpl: Network>
{
pub fn deactivate(self) -> (InactiveW5500<FourWire<ChipSelect>, NetworkImpl>, Spi) {
let (bus, spi) = self.bus.deactivate();
(InactiveW5500::new(bus, self.network), spi)
(InactiveW5500::new(bus, self.network, self.sockets), spi)
}
}
impl<Spi: FullDuplex<u8>, NetworkImpl: Network> W5500<ActiveThreeWire<Spi>, NetworkImpl> {
pub fn deactivate(self) -> (InactiveW5500<ThreeWire, NetworkImpl>, Spi) {
let (bus, spi) = self.bus.deactivate();
(InactiveW5500::new(bus, self.network), spi)
(InactiveW5500::new(bus, self.network, self.sockets), spi)
}
}