Moved packet to incoming_packet, stubbed outgoing_packet
This commit is contained in:
parent
332ab924db
commit
63890a5d5a
3 changed files with 105 additions and 87 deletions
|
|
@ -6,7 +6,7 @@ use crate::socket::Socket;
|
|||
use crate::IpAddress;
|
||||
use crate::register::socketn;
|
||||
|
||||
pub struct UdpPacket<UdpSocket> {
|
||||
pub struct IncomingPacket<UdpSocket> {
|
||||
udp_socket: UdpSocket,
|
||||
address: IpAddress,
|
||||
remote_port: u16,
|
||||
|
|
@ -14,7 +14,7 @@ pub struct UdpPacket<UdpSocket> {
|
|||
write_pointer: u16,
|
||||
}
|
||||
|
||||
impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> UdpPacket<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>>
|
||||
impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> IncomingPacket<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>>
|
||||
{
|
||||
pub fn new(mut udp_socket: UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>) -> Result<Self, SpiBus::Error> {
|
||||
let receive_size = udp_socket.socket.get_receive_size(&mut udp_socket.bus)?;
|
||||
|
|
@ -26,7 +26,7 @@ impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> UdpPacket<
|
|||
let read_pointer = udp_socket.socket.get_rx_read_pointer(&mut udp_socket.bus)?;
|
||||
let mut header = [0u8; 8];
|
||||
block!(udp_socket.bus.transfer_frame(udp_socket.socket.rx_buffer(), read_pointer, false, &mut header))?;
|
||||
Ok(UdpPacket {
|
||||
Ok(Self {
|
||||
udp_socket,
|
||||
address: IpAddress::new(header[0], header[1], header[2], header[3]),
|
||||
remote_port: BigEndian::read_u16(&header[4..5]),
|
||||
|
|
@ -43,11 +43,7 @@ impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> UdpPacket<
|
|||
self.address
|
||||
}
|
||||
|
||||
pub fn read(&mut self) -> Result<u8, SpiBus::Error> {
|
||||
let mut buffer = [0u8];
|
||||
block!(self.udp_socket.bus.transfer_frame(self.udp_socket.socket.rx_buffer(), self.read_pointer, false, &mut buffer))?;
|
||||
Ok(buffer[0])
|
||||
}
|
||||
// TODO add read_all method
|
||||
|
||||
pub fn done(mut self) -> Result<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>, SpiBus::Error> {
|
||||
self.udp_socket.socket.set_rx_read_pointer(&mut self.udp_socket.bus, self.write_pointer)?;
|
||||
|
|
@ -57,7 +53,7 @@ impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> UdpPacket<
|
|||
|
||||
}
|
||||
|
||||
impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> Iterator for UdpPacket<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>> {
|
||||
impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> Iterator for IncomingPacket<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>> {
|
||||
type Item = Result<u8, SpiBus::Error>;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.read_pointer > self.write_pointer {
|
||||
|
|
@ -66,6 +62,7 @@ impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> Iterator f
|
|||
let mut buffer = [0u8];
|
||||
let result = block!(self.udp_socket.bus.transfer_frame(self.udp_socket.socket.rx_buffer(), self.read_pointer, false, &mut buffer));
|
||||
self.read_pointer += 1;
|
||||
// TODO handle looping back?
|
||||
match result {
|
||||
Ok(_) => Some(Ok(buffer[0])),
|
||||
Result::Err(error) => Some(Err(error)),
|
||||
|
|
@ -1,13 +1,16 @@
|
|||
mod inactive_udp_socket;
|
||||
mod packet;
|
||||
mod incoming_packet;
|
||||
mod outgoing_packet;
|
||||
|
||||
use crate::bus::ActiveBus;
|
||||
use crate::network::Network;
|
||||
use crate::socket::{Socket, OwnedSockets};
|
||||
use crate::udp::inactive_udp_socket::InactiveUdpSocket;
|
||||
use crate::udp::packet::UdpPacket;
|
||||
use crate::udp::incoming_packet::IncomingPacket;
|
||||
use crate::udp::outgoing_packet::OutgoingPacket;
|
||||
use crate::w5500::W5500;
|
||||
use crate::register::socketn;
|
||||
use crate::IpAddress;
|
||||
|
||||
pub struct UdpSocket<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> {
|
||||
bus: SpiBus,
|
||||
|
|
@ -40,88 +43,18 @@ impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: 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<UdpPacket<Self>>, SpiBus::Error> {
|
||||
pub fn receive(mut self) -> Result<Option<IncomingPacket<Self>>, SpiBus::Error> {
|
||||
if !self.socket.has_received(&mut self.bus)? {
|
||||
Ok(None)
|
||||
} else {
|
||||
Ok(Some(UdpPacket::new(self)?))
|
||||
Ok(Some(IncomingPacket::new(self)?))
|
||||
}
|
||||
}
|
||||
|
||||
// /// Sends a UDP packet to the specified IP and port, and blocks until it is sent
|
||||
// fn blocking_send(
|
||||
// &mut self,
|
||||
// host: &IpAddress,
|
||||
// host_port: u16,
|
||||
// data: &[u8],
|
||||
// ) -> Result<(), Self::Error> {
|
||||
// let (w5500, UdpSocket(socket)) = self;
|
||||
|
||||
// {
|
||||
// let local_port = w5500.read_u16(socket.at(SocketRegister::LocalPort))?;
|
||||
// let local_port = local_port.to_be_bytes();
|
||||
// let host_port = host_port.to_be_bytes();
|
||||
|
||||
// w5500.write_to(
|
||||
// socket.at(SocketRegister::LocalPort),
|
||||
// &[
|
||||
// local_port[0],
|
||||
// local_port[1], // local port u16
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00, // destination mac
|
||||
// host.address[0],
|
||||
// host.address[1],
|
||||
// host.address[2],
|
||||
// host.address[3], // target IP
|
||||
// host_port[0],
|
||||
// host_port[1], // destination port (5354)
|
||||
// ],
|
||||
// )?;
|
||||
// }
|
||||
|
||||
// let data_length = data.len() as u16;
|
||||
// {
|
||||
// let data_length = data_length.to_be_bytes();
|
||||
|
||||
// // TODO why write [0x00, 0x00] at TxReadPointer at all?
|
||||
// // TODO Is TxWritePointer not sufficient enough?
|
||||
// w5500.write_to(
|
||||
// socket.at(SocketRegister::TxReadPointer),
|
||||
// &[0x00, 0x00, data_length[0], data_length[1]],
|
||||
// );
|
||||
// }
|
||||
|
||||
// w5500.write_to(
|
||||
// socket.tx_register_at(0x00_00),
|
||||
// &data[..data_length as usize],
|
||||
// )?;
|
||||
|
||||
// w5500.write_to(
|
||||
// socket.at(SocketRegister::Command),
|
||||
// &[SocketCommand::Send as u8],
|
||||
// )?;
|
||||
|
||||
// for _ in 0..0xFFFF {
|
||||
// // wait until sent
|
||||
// if w5500.is_interrupt_set(*socket, Interrupt::SendOk)? {
|
||||
// w5500.reset_interrupt(*socket, Interrupt::SendOk)?;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// // restore listen state
|
||||
// w5500.write_to(
|
||||
// socket.at(SocketRegister::Mode),
|
||||
// &[
|
||||
// Protocol::UDP as u8, // Socket Mode Register
|
||||
// SocketCommand::Open as u8, // Socket Command Register
|
||||
// ],
|
||||
// )?;
|
||||
// Ok(())
|
||||
// }
|
||||
/// Sends a UDP packet to the specified IP and port, and blocks until it is sent
|
||||
pub fn send(self, host: IpAddress, remote_port: u16) -> Result<OutgoingPacket<Self>, SpiBus::Error> {
|
||||
Ok(OutgoingPacket::new(self, host, remote_port)?)
|
||||
}
|
||||
|
||||
|
||||
pub fn deactivate(
|
||||
|
|
|
|||
88
src/udp/outgoing_packet.rs
Normal file
88
src/udp/outgoing_packet.rs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
use crate::bus::ActiveBus;
|
||||
use crate::network::Network;
|
||||
use crate::socket::Socket;
|
||||
use crate::IpAddress;
|
||||
use crate::udp::UdpSocket;
|
||||
|
||||
pub struct OutgoingPacket<UdpSocket> {
|
||||
udp_socket: UdpSocket
|
||||
}
|
||||
|
||||
impl<'a, SpiBus: ActiveBus, NetworkImpl: Network, SocketImpl: Socket> OutgoingPacket<UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>> {
|
||||
|
||||
pub fn new(udp_socket: UdpSocket<'a, SpiBus, NetworkImpl, SocketImpl>, host: IpAddress, port: u16) -> Result<Self, SpiBus::Error> {
|
||||
Ok(Self { udp_socket })
|
||||
// let (w5500, UdpSocket(socket)) = self;
|
||||
|
||||
// {
|
||||
|
||||
// // TODO set up packet destination
|
||||
// let local_port = w5500.read_u16(socket.at(SocketRegister::LocalPort))?;
|
||||
// let local_port = local_port.to_be_bytes();
|
||||
// let host_port = host_port.to_be_bytes();
|
||||
|
||||
// w5500.write_to(
|
||||
// socket.at(SocketRegister::LocalPort),
|
||||
// &[
|
||||
// local_port[0],
|
||||
// local_port[1], // local port u16
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00,
|
||||
// 0x00, // destination mac
|
||||
// host.address[0],
|
||||
// host.address[1],
|
||||
// host.address[2],
|
||||
// host.address[3], // target IP
|
||||
// host_port[0],
|
||||
// host_port[1], // destination port (5354)
|
||||
// ],
|
||||
// )?;
|
||||
// }
|
||||
|
||||
// // TODO set read/write pointers
|
||||
// let data_length = data.len() as u16;
|
||||
// {
|
||||
// let data_length = data_length.to_be_bytes();
|
||||
|
||||
// // TODO why write [0x00, 0x00] at TxReadPointer at all?
|
||||
// // TODO Is TxWritePointer not sufficient enough?
|
||||
// w5500.write_to(
|
||||
// socket.at(SocketRegister::TxReadPointer),
|
||||
// &[0x00, 0x00, data_length[0], data_length[1]],
|
||||
// );
|
||||
// }
|
||||
|
||||
// // TODO write body to buffer
|
||||
// w5500.write_to(
|
||||
// socket.tx_register_at(0x00_00),
|
||||
// &data[..data_length as usize],
|
||||
// )?;
|
||||
|
||||
// // TODO execute send command
|
||||
// w5500.write_to(
|
||||
// socket.at(SocketRegister::Command),
|
||||
// &[SocketCommand::Send as u8],
|
||||
// )?;
|
||||
|
||||
// // TODO wait until send is complete
|
||||
// for _ in 0..0xFFFF {
|
||||
// // wait until sent
|
||||
// if w5500.is_interrupt_set(*socket, Interrupt::SendOk)? {
|
||||
// w5500.reset_interrupt(*socket, Interrupt::SendOk)?;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// // TODO listen for incoming sockets
|
||||
// w5500.write_to(
|
||||
// socket.at(SocketRegister::Mode),
|
||||
// &[
|
||||
// Protocol::UDP as u8, // Socket Mode Register
|
||||
// SocketCommand::Open as u8, // Socket Command Register
|
||||
// ],
|
||||
// )?;
|
||||
// Ok(())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue