Added inactive UP socket state, added run-time socket ownership checking, added register addresses to sockets

This commit is contained in:
Jonah Dahlquist 2019-09-04 13:37:59 -05:00 committed by Jonah Dahlquist
commit b82bb92ead
4 changed files with 184 additions and 18 deletions

View file

@ -0,0 +1,25 @@
use crate::socket::Socket;
use crate::w5500::ForeignSocketError;
use crate::udp::UdpSocket;
use crate::w5500::W5500;
use crate::bus::ActiveBus;
use crate::network::Network;
pub struct InactiveUdpSocket<'a, SocketImpl: Socket> {
socket: &'a mut SocketImpl,
}
impl<'a, SocketImpl: Socket> InactiveUdpSocket<'a, SocketImpl> {
pub fn new(socket: &'a mut 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::new(bus, network, sockets, self.socket))
} else {
Err(ForeignSocketError {})
}
}
}

View file

@ -1,12 +1,37 @@
use crate::socket::Socket;
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;
pub struct UdpSocket<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> {
bus: SpiBus,
network: NetworkImpl,
sockets: OwnedSockets,
pub struct UdpSocket<'a, SocketImpl: Socket> {
socket: &'a mut SocketImpl,
}
impl<'a, SocketImpl: Socket> UdpSocket<'a, SocketImpl> {
pub fn new(socket: &'a mut SocketImpl) -> Self {
// TODO initialize socket for UDP mode
UdpSocket { socket }
impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl> {
pub fn new(bus: SpiBus, network: NetworkImpl, sockets: OwnedSockets, socket: &'a mut SocketImpl) -> Self {
// TODO setup socket in UDP mode
// self.0.reset_interrupt(socket, Interrupt::SendOk)?;
// self.0.write_u16(socket.at(SocketRegister::LocalPort), port)?;
// self.0.write_to(
// socket.at(SocketRegister::Mode),
// &[
// Protocol::UDP as u8, // Socket Mode Register
// SocketCommand::Open as u8, // Socket Command Register
// ],
// )?;
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))
}
}