Ran formatting

This commit is contained in:
Jonah Dahlquist 2019-09-05 20:39:47 -05:00 committed by Jonah Dahlquist
commit 95ca1be001
5 changed files with 64 additions and 22 deletions

View file

@ -44,13 +44,13 @@ 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;

View file

@ -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<SpiBus: ActiveBus>(&self, bus: &mut SpiBus, mode: socketn::Protocol) -> Result<(), SpiBus::Error> {
fn set_mode<SpiBus: ActiveBus>(
&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<SpiBus: ActiveBus>(&self, bus: &mut SpiBus, code: socketn::Interrupt) -> Result<(), SpiBus::Error> {
fn reset_interrupt<SpiBus: ActiveBus>(
&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<SpiBus: ActiveBus>(&self, bus: &mut SpiBus, port: u16) -> Result<(), SpiBus::Error> {
fn set_source_port<SpiBus: ActiveBus>(
&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))?;

View file

@ -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<SpiBus: ActiveBus, NetworkImpl: Network>(self, w5500: W5500<SpiBus, NetworkImpl>) -> Result<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>, ForeignSocketError> {
pub fn activate<SpiBus: ActiveBus, NetworkImpl: Network>(
self,
w5500: W5500<SpiBus, NetworkImpl>,
) -> Result<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>, 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 {})
}

View file

@ -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<Self, SpiBus::Error> {
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<Self, SpiBus::Error> {
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<SpiBus, NetworkImpl>) {
(InactiveUdpSocket::new(self.socket), W5500::new(self.bus, self.network, self.sockets))
pub fn deactivate(
self,
) -> (
InactiveUdpSocket<'a, SocketImpl>,
W5500<SpiBus, NetworkImpl>,
) {
(
InactiveUdpSocket::new(self.socket),
W5500::new(self.bus, self.network, self.sockets),
)
}
}

View file

@ -54,7 +54,8 @@ impl<SpiBus: ActiveBus, NetworkImpl: Network> W5500<SpiBus, NetworkImpl> {
self,
port: u16,
socket: &'a mut SocketImpl,
) -> Result<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>, OpenSocketError<SpiBus::Error>> {
) -> Result<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>, OpenSocketError<SpiBus::Error>>
{
if socket.is_owned_by(&self.sockets) {
UdpSocket::new(port, self.bus, self.network, self.sockets, socket)
.map_err(|e| OpenSocketError::BusError(e))