Upgrade to smoltcp v0.11.

This commit is contained in:
Dario Nieuwenhuis 2023-12-23 20:41:33 +01:00
parent 92758c3119
commit f625f6b893
2 changed files with 18 additions and 9 deletions

View file

@ -60,7 +60,7 @@ igmp = ["smoltcp/proto-igmp"]
defmt = { version = "0.3", optional = true }
log = { version = "0.4.14", optional = true }
smoltcp = { git = "https://github.com/smoltcp-rs/smoltcp.git", rev = "b57e2f9e70e82a13f31d5ea17e55232c11cc2b2d", default-features = false, features = [
smoltcp = { version = "0.11.0", default-features = false, features = [
"socket",
"async",
] }

View file

@ -26,13 +26,21 @@ pub enum BindError {
/// Error returned by [`UdpSocket::recv_from`] and [`UdpSocket::send_to`].
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
pub enum SendError {
/// No route to host.
NoRoute,
/// Socket not bound to an outgoing port.
SocketNotBound,
}
/// Error returned by [`UdpSocket::recv_from`] and [`UdpSocket::send_to`].
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum RecvError {
/// Provided buffer was smaller than the received packet.
Truncated,
}
/// An UDP socket.
pub struct UdpSocket<'a> {
stack: &'a RefCell<SocketStack>,
@ -103,7 +111,7 @@ impl<'a> UdpSocket<'a> {
/// This method will wait until a datagram is received.
///
/// Returns the number of bytes received and the remote endpoint.
pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), Error> {
pub async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpEndpoint), RecvError> {
poll_fn(move |cx| self.poll_recv_from(buf, cx)).await
}
@ -114,10 +122,11 @@ impl<'a> UdpSocket<'a> {
///
/// When a datagram is received, this method will return `Poll::Ready` with the
/// number of bytes received and the remote endpoint.
pub fn poll_recv_from(&self, buf: &mut [u8], cx: &mut Context<'_>) -> Poll<Result<(usize, IpEndpoint), Error>> {
pub fn poll_recv_from(&self, buf: &mut [u8], cx: &mut Context<'_>) -> Poll<Result<(usize, IpEndpoint), RecvError>> {
self.with_mut(|s, _| match s.recv_slice(buf) {
Ok((n, meta)) => Poll::Ready(Ok((n, meta.endpoint))),
// No data ready
Err(udp::RecvError::Truncated) => Poll::Ready(Err(RecvError::Truncated)),
Err(udp::RecvError::Exhausted) => {
s.register_recv_waker(cx.waker());
Poll::Pending
@ -129,8 +138,8 @@ impl<'a> UdpSocket<'a> {
///
/// This method will wait until the datagram has been sent.
///
/// When the remote endpoint is not reachable, this method will return `Err(Error::NoRoute)`
pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), Error>
/// When the remote endpoint is not reachable, this method will return `Err(SendError::NoRoute)`
pub async fn send_to<T>(&self, buf: &[u8], remote_endpoint: T) -> Result<(), SendError>
where
T: Into<IpEndpoint>,
{
@ -146,7 +155,7 @@ impl<'a> UdpSocket<'a> {
/// and register the current task to be notified when the buffer has space available.
///
/// When the remote endpoint is not reachable, this method will return `Poll::Ready(Err(Error::NoRoute))`.
pub fn poll_send_to<T>(&self, buf: &[u8], remote_endpoint: T, cx: &mut Context<'_>) -> Poll<Result<(), Error>>
pub fn poll_send_to<T>(&self, buf: &[u8], remote_endpoint: T, cx: &mut Context<'_>) -> Poll<Result<(), SendError>>
where
T: Into<IpEndpoint>,
{
@ -160,9 +169,9 @@ impl<'a> UdpSocket<'a> {
Err(udp::SendError::Unaddressable) => {
// If no sender/outgoing port is specified, there is not really "no route"
if s.endpoint().port == 0 {
Poll::Ready(Err(Error::SocketNotBound))
Poll::Ready(Err(SendError::SocketNotBound))
} else {
Poll::Ready(Err(Error::NoRoute))
Poll::Ready(Err(SendError::NoRoute))
}
}
})