Ran formatting
This commit is contained in:
parent
36df284dca
commit
95ca1be001
5 changed files with 64 additions and 22 deletions
|
|
@ -44,14 +44,14 @@ pub mod socketn {
|
||||||
pub const MODE: u16 = 0x00;
|
pub const MODE: u16 = 0x00;
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum Protocol {
|
pub enum Protocol {
|
||||||
Udp = 0b10u8
|
Udp = 0b10u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const INTERRUPT: u16 = 0x02;
|
pub const INTERRUPT: u16 = 0x02;
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum Interrupt {
|
pub enum Interrupt {
|
||||||
SendOk = 0b10000u8
|
SendOk = 0b10000u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const SOURCE_PORT: u16 = 0x04;
|
pub const SOURCE_PORT: u16 = 0x04;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use byteorder::{BigEndian, ByteOrder};
|
use byteorder::{BigEndian, ByteOrder};
|
||||||
|
|
||||||
use crate::register;
|
|
||||||
use crate::bus::ActiveBus;
|
use crate::bus::ActiveBus;
|
||||||
|
use crate::register;
|
||||||
use crate::register::socketn;
|
use crate::register::socketn;
|
||||||
|
|
||||||
pub trait Socket {
|
pub trait Socket {
|
||||||
|
|
@ -9,17 +9,29 @@ pub trait Socket {
|
||||||
fn register(&self) -> u8;
|
fn register(&self) -> u8;
|
||||||
fn tx_buffer(&self) -> u8;
|
fn tx_buffer(&self) -> u8;
|
||||||
fn rx_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];
|
let mut mode = [mode as u8];
|
||||||
block!(bus.transfer_frame(self.register(), socketn::MODE, true, &mut mode))?;
|
block!(bus.transfer_frame(self.register(), socketn::MODE, true, &mut mode))?;
|
||||||
Ok(())
|
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];
|
let mut data = [code as u8];
|
||||||
block!(bus.transfer_frame(self.register(), socketn::INTERRUPT, true, &mut data))?;
|
block!(bus.transfer_frame(self.register(), socketn::INTERRUPT, true, &mut data))?;
|
||||||
Ok(())
|
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];
|
let mut data = [0u8; 2];
|
||||||
BigEndian::write_u16(&mut data[..], port);
|
BigEndian::write_u16(&mut data[..], port);
|
||||||
block!(bus.transfer_frame(self.register(), socketn::SOURCE_PORT, true, &mut data))?;
|
block!(bus.transfer_frame(self.register(), socketn::SOURCE_PORT, true, &mut data))?;
|
||||||
|
|
|
||||||
|
|
@ -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::bus::ActiveBus;
|
||||||
use crate::network::Network;
|
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> {
|
pub struct InactiveUdpSocket<'a, SocketImpl: Socket> {
|
||||||
socket: &'a mut SocketImpl,
|
socket: &'a mut SocketImpl,
|
||||||
|
|
@ -14,10 +14,18 @@ impl<'a, SocketImpl: Socket> InactiveUdpSocket<'a, SocketImpl> {
|
||||||
InactiveUdpSocket { socket }
|
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();
|
let (bus, network, sockets) = w5500.release();
|
||||||
if self.socket.is_owned_by(&sockets) {
|
if self.socket.is_owned_by(&sockets) {
|
||||||
Ok(UdpSocket { bus, network, sockets, socket: self.socket })
|
Ok(UdpSocket {
|
||||||
|
bus,
|
||||||
|
network,
|
||||||
|
sockets,
|
||||||
|
socket: self.socket,
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
Err(ForeignSocketError {})
|
Err(ForeignSocketError {})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@ mod inactive_udp_socket;
|
||||||
|
|
||||||
use crate::bus::ActiveBus;
|
use crate::bus::ActiveBus;
|
||||||
use crate::network::Network;
|
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::OwnedSockets;
|
||||||
|
use crate::socket::Socket;
|
||||||
|
use crate::udp::inactive_udp_socket::InactiveUdpSocket;
|
||||||
|
use crate::w5500::W5500;
|
||||||
use register::socketn;
|
use register::socketn;
|
||||||
|
|
||||||
pub struct UdpSocket<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> {
|
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,
|
socket: &'a mut SocketImpl,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl> {
|
impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket>
|
||||||
pub fn new(port: u16, mut bus: SpiBus, network: NetworkImpl, sockets: OwnedSockets, socket: &'a mut SocketImpl) -> Result<Self, SpiBus::Error> {
|
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.reset_interrupt(&mut bus, socketn::Interrupt::SendOk)?;
|
||||||
socket.set_source_port(&mut bus, port)?;
|
socket.set_source_port(&mut bus, port)?;
|
||||||
socket.set_mode(&mut bus, socketn::Protocol::Udp)?;
|
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>) {
|
pub fn deactivate(
|
||||||
(InactiveUdpSocket::new(self.socket), W5500::new(self.bus, self.network, self.sockets))
|
self,
|
||||||
|
) -> (
|
||||||
|
InactiveUdpSocket<'a, SocketImpl>,
|
||||||
|
W5500<SpiBus, NetworkImpl>,
|
||||||
|
) {
|
||||||
|
(
|
||||||
|
InactiveUdpSocket::new(self.socket),
|
||||||
|
W5500::new(self.bus, self.network, self.sockets),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,8 @@ impl<SpiBus: ActiveBus, NetworkImpl: Network> W5500<SpiBus, NetworkImpl> {
|
||||||
self,
|
self,
|
||||||
port: u16,
|
port: u16,
|
||||||
socket: &'a mut SocketImpl,
|
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) {
|
if socket.is_owned_by(&self.sockets) {
|
||||||
UdpSocket::new(port, self.bus, self.network, self.sockets, socket)
|
UdpSocket::new(port, self.bus, self.network, self.sockets, socket)
|
||||||
.map_err(|e| OpenSocketError::BusError(e))
|
.map_err(|e| OpenSocketError::BusError(e))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue