From 95ca1be001bb81d1d317bd23068d2377461c8102 Mon Sep 17 00:00:00 2001 From: Jonah Dahlquist Date: Thu, 5 Sep 2019 20:39:47 -0500 Subject: [PATCH] Ran formatting --- src/register.rs | 6 +++--- src/socket/mod.rs | 20 ++++++++++++++---- src/udp/inactive_udp_socket.rs | 20 ++++++++++++------ src/udp/mod.rs | 37 ++++++++++++++++++++++++++-------- src/w5500.rs | 3 ++- 5 files changed, 64 insertions(+), 22 deletions(-) diff --git a/src/register.rs b/src/register.rs index 835b42a..4cf59e0 100644 --- a/src/register.rs +++ b/src/register.rs @@ -44,14 +44,14 @@ pub mod socketn { pub const MODE: u16 = 0x00; #[repr(u8)] pub enum Protocol { - Udp = 0b10u8 + Udp = 0b10u8, } pub const INTERRUPT: u16 = 0x02; #[repr(u8)] pub enum Interrupt { - SendOk = 0b10000u8 + SendOk = 0b10000u8, } pub const SOURCE_PORT: u16 = 0x04; -} \ No newline at end of file +} diff --git a/src/socket/mod.rs b/src/socket/mod.rs index 5cda10a..e561a1e 100644 --- a/src/socket/mod.rs +++ b/src/socket/mod.rs @@ -1,7 +1,7 @@ use byteorder::{BigEndian, ByteOrder}; -use crate::register; use crate::bus::ActiveBus; +use crate::register; use crate::register::socketn; pub trait Socket { @@ -9,17 +9,29 @@ pub trait Socket { fn register(&self) -> u8; fn tx_buffer(&self) -> u8; fn rx_buffer(&self) -> u8; - fn set_mode(&self, bus: &mut SpiBus, mode: socketn::Protocol) -> Result<(), SpiBus::Error> { + fn set_mode( + &self, + bus: &mut SpiBus, + mode: socketn::Protocol, + ) -> Result<(), SpiBus::Error> { let mut mode = [mode as u8]; block!(bus.transfer_frame(self.register(), socketn::MODE, true, &mut mode))?; Ok(()) } - fn reset_interrupt(&self, bus: &mut SpiBus, code: socketn::Interrupt) -> Result<(), SpiBus::Error> { + fn reset_interrupt( + &self, + bus: &mut SpiBus, + code: socketn::Interrupt, + ) -> Result<(), SpiBus::Error> { let mut data = [code as u8]; block!(bus.transfer_frame(self.register(), socketn::INTERRUPT, true, &mut data))?; Ok(()) } - fn set_source_port(&self, bus: &mut SpiBus, port: u16) -> Result<(), SpiBus::Error> { + fn set_source_port( + &self, + bus: &mut SpiBus, + port: u16, + ) -> Result<(), SpiBus::Error> { let mut data = [0u8; 2]; BigEndian::write_u16(&mut data[..], port); block!(bus.transfer_frame(self.register(), socketn::SOURCE_PORT, true, &mut data))?; diff --git a/src/udp/inactive_udp_socket.rs b/src/udp/inactive_udp_socket.rs index 7557894..fc739ee 100644 --- a/src/udp/inactive_udp_socket.rs +++ b/src/udp/inactive_udp_socket.rs @@ -1,9 +1,9 @@ -use crate::socket::Socket; -use crate::w5500::ForeignSocketError; -use crate::udp::UdpSocket; -use crate::w5500::W5500; use crate::bus::ActiveBus; use crate::network::Network; +use crate::socket::Socket; +use crate::udp::UdpSocket; +use crate::w5500::ForeignSocketError; +use crate::w5500::W5500; pub struct InactiveUdpSocket<'a, SocketImpl: Socket> { socket: &'a mut SocketImpl, @@ -14,10 +14,18 @@ impl<'a, SocketImpl: Socket> InactiveUdpSocket<'a, SocketImpl> { InactiveUdpSocket { socket } } - pub fn activate(self, w5500: W5500) -> Result, ForeignSocketError> { + pub fn activate( + self, + w5500: W5500, + ) -> Result, ForeignSocketError> { let (bus, network, sockets) = w5500.release(); if self.socket.is_owned_by(&sockets) { - Ok(UdpSocket { bus, network, sockets, socket: self.socket }) + Ok(UdpSocket { + bus, + network, + sockets, + socket: self.socket, + }) } else { Err(ForeignSocketError {}) } diff --git a/src/udp/mod.rs b/src/udp/mod.rs index 1729e57..2b31075 100644 --- a/src/udp/mod.rs +++ b/src/udp/mod.rs @@ -2,10 +2,10 @@ mod inactive_udp_socket; use crate::bus::ActiveBus; use crate::network::Network; -use crate::socket::Socket; -use crate::w5500::W5500; -use crate::udp::inactive_udp_socket::InactiveUdpSocket; use crate::socket::OwnedSockets; +use crate::socket::Socket; +use crate::udp::inactive_udp_socket::InactiveUdpSocket; +use crate::w5500::W5500; use register::socketn; pub struct UdpSocket<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> { @@ -16,16 +16,37 @@ pub struct UdpSocket<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: So socket: &'a mut SocketImpl, } -impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl> { - pub fn new(port: u16, mut bus: SpiBus, network: NetworkImpl, sockets: OwnedSockets, socket: &'a mut SocketImpl) -> Result { +impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> + UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl> +{ + pub fn new( + port: u16, + mut bus: SpiBus, + network: NetworkImpl, + sockets: OwnedSockets, + socket: &'a mut SocketImpl, + ) -> Result { socket.reset_interrupt(&mut bus, socketn::Interrupt::SendOk)?; socket.set_source_port(&mut bus, port)?; socket.set_mode(&mut bus, socketn::Protocol::Udp)?; - Ok(UdpSocket { bus, network, sockets, socket }) + Ok(UdpSocket { + bus, + network, + sockets, + socket, + }) } - pub fn deactivate(self) -> (InactiveUdpSocket<'a, SocketImpl>, W5500) { - (InactiveUdpSocket::new(self.socket), W5500::new(self.bus, self.network, self.sockets)) + pub fn deactivate( + self, + ) -> ( + InactiveUdpSocket<'a, SocketImpl>, + W5500, + ) { + ( + InactiveUdpSocket::new(self.socket), + W5500::new(self.bus, self.network, self.sockets), + ) } } diff --git a/src/w5500.rs b/src/w5500.rs index 2f4071f..d9d8d68 100644 --- a/src/w5500.rs +++ b/src/w5500.rs @@ -54,7 +54,8 @@ impl W5500 { self, port: u16, socket: &'a mut SocketImpl, - ) -> Result, OpenSocketError> { + ) -> Result, OpenSocketError> + { if socket.is_owned_by(&self.sockets) { UdpSocket::new(port, self.bus, self.network, self.sockets, socket) .map_err(|e| OpenSocketError::BusError(e))