2022-12-03 00:56:16 +01:00
|
|
|
use core::cell::RefCell;
|
2022-09-22 16:42:49 +02:00
|
|
|
use core::future::poll_fn;
|
2022-07-28 10:25:47 +02:00
|
|
|
use core::mem;
|
|
|
|
use core::task::Poll;
|
|
|
|
|
|
|
|
use smoltcp::iface::{Interface, SocketHandle};
|
|
|
|
use smoltcp::socket::udp::{self, PacketMetadata};
|
|
|
|
use smoltcp::wire::{IpEndpoint, IpListenEndpoint};
|
|
|
|
|
|
|
|
use super::stack::SocketStack;
|
|
|
|
use crate::{Device, Stack};
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum BindError {
|
|
|
|
/// The socket was already open.
|
|
|
|
InvalidState,
|
|
|
|
/// No route to host.
|
|
|
|
NoRoute,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
pub enum Error {
|
|
|
|
/// No route to host.
|
|
|
|
NoRoute,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct UdpSocket<'a> {
|
2022-12-03 00:56:16 +01:00
|
|
|
stack: &'a RefCell<SocketStack>,
|
2022-08-11 15:52:32 +02:00
|
|
|
handle: SocketHandle,
|
2022-07-28 10:25:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> UdpSocket<'a> {
|
|
|
|
pub fn new<D: Device>(
|
|
|
|
stack: &'a Stack<D>,
|
|
|
|
rx_meta: &'a mut [PacketMetadata],
|
|
|
|
rx_buffer: &'a mut [u8],
|
|
|
|
tx_meta: &'a mut [PacketMetadata],
|
|
|
|
tx_buffer: &'a mut [u8],
|
|
|
|
) -> Self {
|
2022-12-03 00:56:16 +01:00
|
|
|
let s = &mut *stack.socket.borrow_mut();
|
2022-07-28 10:25:47 +02:00
|
|
|
|
|
|
|
let rx_meta: &'static mut [PacketMetadata] = unsafe { mem::transmute(rx_meta) };
|
|
|
|
let rx_buffer: &'static mut [u8] = unsafe { mem::transmute(rx_buffer) };
|
|
|
|
let tx_meta: &'static mut [PacketMetadata] = unsafe { mem::transmute(tx_meta) };
|
|
|
|
let tx_buffer: &'static mut [u8] = unsafe { mem::transmute(tx_buffer) };
|
|
|
|
let handle = s.sockets.add(udp::Socket::new(
|
|
|
|
udp::PacketBuffer::new(rx_meta, rx_buffer),
|
|
|
|
udp::PacketBuffer::new(tx_meta, tx_buffer),
|
|
|
|
));
|
|
|
|
|
|
|
|
Self {
|
2022-08-11 15:52:32 +02:00
|
|
|
stack: &stack.socket,
|
|
|
|
handle,
|
2022-07-28 10:25:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bind<T>(&mut self, endpoint: T) -> Result<(), BindError>
|
|
|
|
where
|
|
|
|
T: Into<IpListenEndpoint>,
|
|
|
|
{
|
|
|
|
let mut endpoint = endpoint.into();
|
|
|
|
|
|
|
|
if endpoint.port == 0 {
|
|
|
|
// If user didn't specify port allocate a dynamic port.
|
2022-12-03 00:56:16 +01:00
|
|
|
endpoint.port = self.stack.borrow_mut().get_local_port();
|
2022-07-28 10:25:47 +02:00
|
|
|
}
|
|
|
|
|
2022-12-03 00:56:16 +01:00
|
|
|
match self.with_mut(|s, _| s.bind(endpoint)) {
|
2022-07-28 10:25:47 +02:00
|
|
|
Ok(()) => Ok(()),
|
|
|
|
Err(udp::BindError::InvalidState) => Err(BindError::InvalidState),
|
|
|
|
Err(udp::BindError::Unaddressable) => Err(BindError::NoRoute),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-03 00:56:16 +01:00
|
|
|
fn with<R>(&self, f: impl FnOnce(&udp::Socket, &Interface) -> R) -> R {
|
|
|
|
let s = &*self.stack.borrow();
|
2022-07-28 10:25:47 +02:00
|
|
|
let socket = s.sockets.get::<udp::Socket>(self.handle);
|
|
|
|
f(socket, &s.iface)
|
|
|
|
}
|
|
|
|
|
2022-12-03 00:56:16 +01:00
|
|
|
fn with_mut<R>(&self, f: impl FnOnce(&mut udp::Socket, &mut Interface) -> R) -> R {
|
|
|
|
let s = &mut *self.stack.borrow_mut();
|
2022-07-28 10:25:47 +02:00
|
|
|
let socket = s.sockets.get_mut::<udp::Socket>(self.handle);
|
|
|
|
let res = f(socket, &mut s.iface);
|
|
|
|
s.waker.wake();
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2022-08-11 15:52:32 +02:00
|
|
|
pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> {
|
2022-12-03 00:56:16 +01:00
|
|
|
poll_fn(move |cx| {
|
2022-07-28 10:25:47 +02:00
|
|
|
self.with_mut(|s, _| match s.recv_slice(buf) {
|
|
|
|
Ok(x) => Poll::Ready(Ok(x)),
|
|
|
|
// No data ready
|
|
|
|
Err(udp::RecvError::Exhausted) => {
|
|
|
|
//s.register_recv_waker(cx.waker());
|
|
|
|
cx.waker().wake_by_ref();
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2022-08-11 15:52:32 +02:00
|
|
|
pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
T: Into<IpEndpoint>,
|
|
|
|
{
|
|
|
|
let remote_endpoint = remote_endpoint.into();
|
2022-12-03 00:56:16 +01:00
|
|
|
poll_fn(move |cx| {
|
2022-08-11 15:52:32 +02:00
|
|
|
self.with_mut(|s, _| match s.send_slice(buf, remote_endpoint) {
|
2022-07-28 10:25:47 +02:00
|
|
|
// Entire datagram has been sent
|
|
|
|
Ok(()) => Poll::Ready(Ok(())),
|
|
|
|
Err(udp::SendError::BufferFull) => {
|
|
|
|
s.register_send_waker(cx.waker());
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
Err(udp::SendError::Unaddressable) => Poll::Ready(Err(Error::NoRoute)),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
2022-08-11 15:52:32 +02:00
|
|
|
|
|
|
|
pub fn endpoint(&self) -> IpListenEndpoint {
|
2022-12-03 00:56:16 +01:00
|
|
|
self.with(|s, _| s.endpoint())
|
2022-08-11 15:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_open(&self) -> bool {
|
2022-12-03 00:56:16 +01:00
|
|
|
self.with(|s, _| s.is_open())
|
2022-08-11 15:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close(&mut self) {
|
2022-12-03 00:56:16 +01:00
|
|
|
self.with_mut(|s, _| s.close())
|
2022-08-11 15:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn may_send(&self) -> bool {
|
2022-12-03 00:56:16 +01:00
|
|
|
self.with(|s, _| s.can_send())
|
2022-08-11 15:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn may_recv(&self) -> bool {
|
2022-12-03 00:56:16 +01:00
|
|
|
self.with(|s, _| s.can_recv())
|
2022-08-11 15:52:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for UdpSocket<'_> {
|
|
|
|
fn drop(&mut self) {
|
2022-12-03 00:56:16 +01:00
|
|
|
self.stack.borrow_mut().sockets.remove(self.handle);
|
2022-08-11 15:52:32 +02:00
|
|
|
}
|
2022-07-28 10:25:47 +02:00
|
|
|
}
|