From b30e4d0d34080b5464e6a1fddd7a14080ce801d6 Mon Sep 17 00:00:00 2001 From: Jonah Dahlquist Date: Tue, 13 Aug 2019 20:03:02 -0500 Subject: [PATCH] Stubbed UdpSocket struct, added Socket structs --- src/inactive_w5500.rs | 14 ++++++++++---- src/lib.rs | 8 +++++--- src/network/dhcp.rs | 1 + src/socket/mod.rs | 39 ++++++++++++++++++++++++++++++++++++++ src/udp/mod.rs | 5 +++++ src/uninitialized_w5500.rs | 14 ++++++++++++-- src/w5500.rs | 34 +++++++++++++++++++++++++++------ 7 files changed, 100 insertions(+), 15 deletions(-) create mode 100644 src/socket/mod.rs create mode 100644 src/udp/mod.rs diff --git a/src/inactive_w5500.rs b/src/inactive_w5500.rs index 980f2b3..2e7b0b3 100644 --- a/src/inactive_w5500.rs +++ b/src/inactive_w5500.rs @@ -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 { bus: SpiBus, network: NetworkImpl, + sockets: OwnedSockets, } impl InactiveW5500 { - 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 InactiveW5500 W5500, NetworkImpl> { - W5500::new(self.bus.activate(spi), self.network) + W5500::new(self.bus.activate(spi), self.network, self.sockets) } } @@ -29,6 +35,6 @@ impl InactiveW5500 { self, spi: Spi, ) -> W5500, NetworkImpl> { - W5500::new(self.bus.activate(spi), self.network) + W5500::new(self.bus.activate(spi), self.network, self.sockets) } } diff --git a/src/lib.rs b/src/lib.rs index 8ef5045..fdbed97 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/network/dhcp.rs b/src/network/dhcp.rs index 3d13503..f9ba1d2 100644 --- a/src/network/dhcp.rs +++ b/src/network/dhcp.rs @@ -24,6 +24,7 @@ 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/mod.rs b/src/socket/mod.rs new file mode 100644 index 0000000..5f6482f --- /dev/null +++ b/src/socket/mod.rs @@ -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 {} diff --git a/src/udp/mod.rs b/src/udp/mod.rs new file mode 100644 index 0000000..dafc5c7 --- /dev/null +++ b/src/udp/mod.rs @@ -0,0 +1,5 @@ +use crate::socket::Socket; + +pub struct UdpSocket { + pub socket: SocketImpl, +} diff --git a/src/uninitialized_w5500.rs b/src/uninitialized_w5500.rs index 96fb2ac..6b665ac 100644 --- a/src/uninitialized_w5500.rs +++ b/src/uninitialized_w5500.rs @@ -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 { @@ -64,8 +65,17 @@ impl UninitializedW5500 { 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( diff --git a/src/w5500.rs b/src/w5500.rs index 76ff02b..669c905 100644 --- a/src/w5500.rs +++ b/src/w5500.rs @@ -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 { bus: SpiBus, network: NetworkImpl, + sockets: OwnedSockets, } impl W5500 { - 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, SpiBus::Error> { - // TODO accept all sockets back self.clear_mode()?; Ok(UninitializedW5500::new(self.bus)) } @@ -30,7 +50,9 @@ impl W5500 { Ok(()) } - //TODO open_udp_socket + pub fn open_udp_socket(&self, socket: SocketImpl) -> UdpSocket { + UdpSocket { socket } + } } impl, ChipSelect: OutputPin, NetworkImpl: Network> @@ -38,13 +60,13 @@ impl, ChipSelect: OutputPin, NetworkImpl: Network> { pub fn deactivate(self) -> (InactiveW5500, NetworkImpl>, Spi) { let (bus, spi) = self.bus.deactivate(); - (InactiveW5500::new(bus, self.network), spi) + (InactiveW5500::new(bus, self.network, self.sockets), spi) } } impl, NetworkImpl: Network> W5500, NetworkImpl> { pub fn deactivate(self) -> (InactiveW5500, Spi) { let (bus, spi) = self.bus.deactivate(); - (InactiveW5500::new(bus, self.network), spi) + (InactiveW5500::new(bus, self.network, self.sockets), spi) } }