Removed socket ownership checking, giving up on that effort for now

This commit is contained in:
Jonah Dahlquist 2019-09-19 10:00:36 -05:00 committed by Jonah Dahlquist
commit b6a52cbf8e
10 changed files with 81 additions and 171 deletions

View file

@ -2,32 +2,24 @@ 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,
pub struct InactiveUdpSocket<SocketImpl: Socket> {
socket: SocketImpl,
}
impl<'a, SocketImpl: Socket> InactiveUdpSocket<'a, SocketImpl> {
pub fn new(socket: &'a mut SocketImpl) -> Self {
impl<SocketImpl: Socket> InactiveUdpSocket<SocketImpl> {
pub fn new(socket: SocketImpl) -> Self {
InactiveUdpSocket { socket }
}
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,
})
} else {
Err(ForeignSocketError {})
) -> UdpSocket<SpiBus, NetworkImpl, SocketImpl> {
UdpSocket {
w5500,
socket: self.socket,
}
}
}

View file

@ -14,21 +14,25 @@ pub struct IncomingPacket<UdpSocket> {
write_pointer: u16,
}
impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket>
IncomingPacket<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>>
impl<SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket>
IncomingPacket<UdpSocket<SpiBus, NetworkImpl, SocketImpl>>
{
pub fn new(
mut udp_socket: UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>,
mut udp_socket: UdpSocket<SpiBus, NetworkImpl, SocketImpl>,
) -> Result<Self, SpiBus::Error> {
let receive_size = udp_socket.socket.get_receive_size(&mut udp_socket.bus)?;
let receive_size = udp_socket
.socket
.get_receive_size(&mut udp_socket.w5500.bus)?;
// Packet frame, as described in W5200 docs sectino 5.2.2.1
// |<-- read_pointer read_pointer + received_size -->|
// |Destination IP Address | Destination Port | Byte Size of DATA | Actual DATA ... |
// | --- 4 Bytes --- | --- 2 Bytes --- | --- 2 Bytes --- | .... |
let read_pointer = udp_socket.socket.get_rx_read_pointer(&mut udp_socket.bus)?;
let read_pointer = udp_socket
.socket
.get_rx_read_pointer(&mut udp_socket.w5500.bus)?;
let mut header = [0u8; 8];
block!(udp_socket.bus.transfer_frame(
block!(udp_socket.w5500.bus.transfer_frame(
udp_socket.socket.rx_buffer(),
read_pointer,
false,
@ -53,19 +57,19 @@ impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket>
// TODO add read_all method
pub fn done(mut self) -> Result<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>, SpiBus::Error> {
pub fn done(mut self) -> Result<UdpSocket<SpiBus, NetworkImpl, SocketImpl>, SpiBus::Error> {
self.udp_socket
.socket
.set_rx_read_pointer(&mut self.udp_socket.bus, self.write_pointer)?;
.set_rx_read_pointer(&mut self.udp_socket.w5500.bus, self.write_pointer)?;
self.udp_socket
.socket
.command(&mut self.udp_socket.bus, socketn::Command::Receive)?;
.command(&mut self.udp_socket.w5500.bus, socketn::Command::Receive)?;
Ok(self.udp_socket)
}
}
impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> Iterator
for IncomingPacket<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>>
impl<SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> Iterator
for IncomingPacket<UdpSocket<SpiBus, NetworkImpl, SocketImpl>>
{
type Item = Result<u8, SpiBus::Error>;
fn next(&mut self) -> Option<Self::Item> {
@ -73,7 +77,7 @@ impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> Iterator
return None;
}
let mut buffer = [0u8];
let result = block!(self.udp_socket.bus.transfer_frame(
let result = block!(self.udp_socket.w5500.bus.transfer_frame(
self.udp_socket.socket.rx_buffer(),
self.read_pointer,
false,

View file

@ -5,49 +5,40 @@ mod outgoing_packet;
use crate::bus::ActiveBus;
use crate::network::Network;
use crate::register::socketn;
use crate::socket::{OwnedSockets, Socket};
use crate::socket::Socket;
use crate::udp::inactive_udp_socket::InactiveUdpSocket;
use crate::udp::incoming_packet::IncomingPacket;
use crate::udp::outgoing_packet::OutgoingPacket;
use crate::w5500::W5500;
use crate::IpAddress;
pub struct UdpSocket<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> {
bus: SpiBus,
network: NetworkImpl,
sockets: OwnedSockets,
pub struct UdpSocket<SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> {
w5500: W5500<SpiBus, NetworkImpl>,
socket: &'a mut SocketImpl,
socket: SocketImpl,
}
impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket>
UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>
impl<SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket>
UdpSocket<SpiBus, NetworkImpl, SocketImpl>
{
pub fn new(
port: u16,
mut bus: SpiBus,
network: NetworkImpl,
sockets: OwnedSockets,
socket: &'a mut SocketImpl,
mut w5500: W5500<SpiBus, NetworkImpl>,
socket: 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)?;
socket.command(&mut bus, socketn::Command::Open)?;
socket.reset_interrupt(&mut w5500.bus, socketn::Interrupt::SendOk)?;
socket.set_source_port(&mut w5500.bus, port)?;
socket.set_mode(&mut w5500.bus, socketn::Protocol::Udp)?;
socket.command(&mut w5500.bus, socketn::Command::Open)?;
Ok(UdpSocket {
bus,
network,
sockets,
socket,
})
Ok(UdpSocket { w5500, socket })
}
/// Returns a UDP packet if one is available. Will return `None` if no UDP packets are in the socket's buffer
pub fn receive(mut self) -> Result<Option<IncomingPacket<Self>>, SpiBus::Error> {
if !self
.socket
.has_interrupt(&mut self.bus, socketn::Interrupt::Receive)?
.has_interrupt(&mut self.w5500.bus, socketn::Interrupt::Receive)?
{
Ok(None)
} else {
@ -64,15 +55,7 @@ impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket>
Ok(OutgoingPacket::new(self, host, remote_port)?)
}
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<SocketImpl>, W5500<SpiBus, NetworkImpl>) {
(InactiveUdpSocket::new(self.socket), self.w5500)
}
}

View file

@ -10,23 +10,23 @@ pub struct OutgoingPacket<UdpSocket> {
data_length: u16,
}
impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket>
OutgoingPacket<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>>
impl<SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket>
OutgoingPacket<UdpSocket<SpiBus, NetworkImpl, SocketImpl>>
{
pub fn new(
mut udp_socket: UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>,
mut udp_socket: UdpSocket<SpiBus, NetworkImpl, SocketImpl>,
host: IpAddress,
port: u16,
) -> Result<Self, SpiBus::Error> {
udp_socket
.socket
.set_destination_ip(&mut udp_socket.bus, host)?;
.set_destination_ip(&mut udp_socket.w5500.bus, host)?;
udp_socket
.socket
.set_destination_port(&mut udp_socket.bus, port)?;
.set_destination_port(&mut udp_socket.w5500.bus, port)?;
udp_socket
.socket
.set_tx_read_pointer(&mut udp_socket.bus, 0)?;
.set_tx_read_pointer(&mut udp_socket.w5500.bus, 0)?;
Ok(Self {
udp_socket,
data_length: 0,
@ -34,7 +34,7 @@ impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket>
}
pub fn write(&mut self, mut data: &mut [u8]) -> Result<(), SpiBus::Error> {
block!(self.udp_socket.bus.transfer_frame(
block!(self.udp_socket.w5500.bus.transfer_frame(
self.udp_socket.socket.tx_buffer(),
self.data_length,
true,
@ -44,29 +44,29 @@ impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket>
Ok(())
}
pub fn send(mut self) -> Result<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>, SpiBus::Error> {
pub fn send(mut self) -> Result<UdpSocket<SpiBus, NetworkImpl, SocketImpl>, SpiBus::Error> {
self.udp_socket
.socket
.set_tx_write_pointer(&mut self.udp_socket.bus, self.data_length)?;
.set_tx_write_pointer(&mut self.udp_socket.w5500.bus, self.data_length)?;
self.udp_socket
.socket
.command(&mut self.udp_socket.bus, socketn::Command::Send)?;
.command(&mut self.udp_socket.w5500.bus, socketn::Command::Send)?;
loop {
// wait until send is complete
if self
.udp_socket
.socket
.has_interrupt(&mut self.udp_socket.bus, socketn::Interrupt::SendOk)?
.has_interrupt(&mut self.udp_socket.w5500.bus, socketn::Interrupt::SendOk)?
{
self.udp_socket
.socket
.reset_interrupt(&mut self.udp_socket.bus, socketn::Interrupt::SendOk)?;
.reset_interrupt(&mut self.udp_socket.w5500.bus, socketn::Interrupt::SendOk)?;
break;
}
}
self.udp_socket
.socket
.command(&mut self.udp_socket.bus, socketn::Command::Open)?;
.command(&mut self.udp_socket.w5500.bus, socketn::Command::Open)?;
Ok(self.udp_socket)
}
}