1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#![allow(clippy::needless_lifetimes)]

use async_std::{io, net};
use futures::{future, Future};

use crate::{conversions::SocketAddr, SocketState};

/// A UDP socket
pub struct UdpSocket {
    state: SocketState<net::UdpSocket, net::UdpSocket>,
}

impl UdpSocket {
    /// New unconnected socket
    pub(crate) fn new() -> Self {
        Self {
            state: SocketState::Closed,
        }
    }

    /// Create a new already connected socket
    /// Useful for socket features not exposed by embedded-nal
    pub fn connected(socket: net::UdpSocket) -> Self {
        Self {
            state: SocketState::Connected(socket),
        }
    }

    /// Create an already bounded socket
    /// Useful for socket features not exposed by embedded-nal
    pub fn bound(socket: net::UdpSocket) -> Self {
        Self {
            state: SocketState::Bound(socket),
        }
    }
}

impl embedded_nal_async::UdpClientStack for crate::Stack {
    type UdpSocket = UdpSocket;

    type Error = io::Error;

    type SocketFuture<'m> = future::Ready<io::Result<Self::UdpSocket>>
	where
		Self: 'm;

    fn socket<'m>(&'m mut self) -> Self::SocketFuture<'m> {
        future::ready(Ok(UdpSocket::new()))
    }

    type ConnectFuture<'m> = impl Future<Output = Result<(), Self::Error>>
    where
        Self: 'm;

    fn connect<'m>(
        &'m mut self,
        socket: &'m mut Self::UdpSocket,
        remote: embedded_nal_async::SocketAddr,
    ) -> Self::ConnectFuture<'m> {
        async move {
            let unspecified = match remote {
                embedded_nal_async::SocketAddr::V4(_) => {
                    net::SocketAddr::new(net::IpAddr::V4(net::Ipv4Addr::UNSPECIFIED), 0)
                }
                embedded_nal_async::SocketAddr::V6(_) => {
                    net::SocketAddr::new(net::IpAddr::V6(net::Ipv6Addr::UNSPECIFIED), 0)
                }
            };
            let addrs: SocketAddr = remote.into();
            let s = net::UdpSocket::bind(unspecified).await?;
            s.connect(addrs.0).await?;
            socket.state = SocketState::Connected(s);
            Ok(())
        }
    }

    type SendFuture<'m> = impl Future<Output = Result<(), Self::Error>>
    where
        Self: 'm;

    fn send<'m>(
        &'m mut self,
        socket: &'m mut Self::UdpSocket,
        buffer: &'m [u8],
    ) -> Self::SendFuture<'m> {
        async move {
            socket.state.get_either()?.send(buffer).await?;
            Ok(())
        }
    }

    type ReceiveFuture<'m> = impl Future<Output = Result<(usize, embedded_nal_async::SocketAddr), Self::Error>>
    where
        Self: 'm;

    fn receive<'m>(
        &'m mut self,
        socket: &'m mut Self::UdpSocket,
        buffer: &'m mut [u8],
    ) -> Self::ReceiveFuture<'m> {
        async move {
            let (len, addr) = socket.state.get_either()?.recv_from(buffer).await?;
            let addr = SocketAddr(addr);
            Ok((len, addr.into()))
        }
    }

    type CloseFuture<'m> = futures::future::Ready<Result<(), Self::Error>>
    where
        Self: 'm;

    fn close<'m>(&'m mut self, _socket: Self::UdpSocket) -> Self::CloseFuture<'m> {
        future::ready(Ok(()))
    }
}

impl embedded_nal_async::UdpFullStack for crate::Stack {
    type BindFuture<'m> = impl Future<Output = Result<(), Self::Error>>
	where
		Self: 'm;

    fn bind<'m>(
        &'m mut self,
        socket: &'m mut Self::UdpSocket,
        local_port: u16,
    ) -> Self::BindFuture<'m> {
        async move {
            let unspecified = net::SocketAddr::new(self.ip, local_port);
            let s = net::UdpSocket::bind(unspecified).await?;
            socket.state = SocketState::Bound(s);
            Ok(())
        }
    }

    type SendToFuture<'m> = impl Future<Output = Result<(), Self::Error>>
	where
		Self: 'm;

    fn send_to<'m>(
        &'m mut self,
        socket: &'m mut Self::UdpSocket,
        remote: embedded_nal_async::SocketAddr,
        buffer: &'m [u8],
    ) -> Self::SendToFuture<'m> {
        async move {
            let addrs: SocketAddr = remote.into();
            socket.state.get_bound()?.send_to(buffer, addrs.0).await?;
            Ok(())
        }
    }
}